一、对象

  1. // 字面量
  2. var teacher = {
  3. name: '张三',
  4. age: 32,
  5. sex: 'male',
  6. height: 176,
  7. weight: 130,
  8. teach: function () {
  9. //就是函数,在对象中不能叫函数了,叫方法
  10. console.log('I am teaching JavaScript');
  11. },
  12. smoke: function () {
  13. console.log('I am smoking');
  14. },
  15. eat: function () {
  16. console.log('I am having a dinner');
  17. }
  18. }
  19. // 增加属性/方法
  20. teacher.email = '350604967@qq.com';
  21. teacher.drink = function () {
  22. console.log('I am drinking');
  23. }
  24. // 修改属性/方法
  25. teacher.age = '25';
  26. teacher.teach = function () {
  27. console.log('I am teaching HTML');
  28. }
  29. // 删除属性/方法
  30. delete teacher.name;
  31. delete teacher.eat;
  32. console.log(teacher);
  33. //--------------------------------------------------------------------------
  1. var teacher = {
  2. name: "张三",
  3. age: 32,
  4. sex: "male",
  5. height: 176,
  6. weight: 130,
  7. teach: function () {
  8. //就是函数,在对象中不能叫函数了,叫方法
  9. console.log("I am teaching JavaScript");
  10. },
  11. smoke: function () {
  12. teacher.weight--;
  13. console.log(teacher.weight); //正确 129 128 129
  14. // console.log(weight); //错误Uncaught ReferenceError: weight is not defined
  15. // console.log(this.weight); // 正确与teacher.weight一样
  16. },
  17. eat: function () {
  18. teacher.weight++;
  19. console.log(teacher.weight);
  20. },
  21. };
  22. teacher.smoke();
  23. teacher.smoke();
  24. teacher.eat();

this指代对象本身

对象function函数,才有this
字面量是对象,有this,里面的函数有this,变量可以接收this,因为attendance是对象,它里面就有this

  1. var attendance = {
  2. students: [],
  3. total: 6,
  4. join: function(name) {
  5. this.students.push(name);
  6. if (this.students.length === this.total) {
  7. console.log(name + '到课,学生已到齐');
  8. } else {
  9. console.log(name + '到课,学生未到齐');
  10. }
  11. },
  12. leave: function(name) {
  13. var idx = this.students.indexOf(name);
  14. if (idx !== -1) {
  15. this.students.splice(idx, 1);
  16. }
  17. console.log(name + '早退');
  18. console.log(this.students);
  19. },
  20. classOver: function() {
  21. this.students = [];
  22. console.log('已下课');
  23. }
  24. }
  25. attendance.join('张三');
  26. attendance.join('李四');
  27. attendance.join('王五');
  28. attendance.join('赵六');
  29. attendance.join('小红');
  30. attendance.join('小明');
  31. attendance.leave('王五');
  32. attendance.classOver();

方法像函数一样,可以添加参数

二、构造函数


1、自定义构造函数 - 大驼峰命名
2、通过 new 关键字创建实例化对象
3、每次 new 创建的对象都是不同的对象
4、this 指向实例化对象

自带构造函数

  1. var obj=new Object();//与对象字面量相等
  2. obj.name='张三';
  3. obj.sex='男士';
  4. console.log(obj)

系统自带的构造函数object

自定义构造函数

只靠系统自带的构造函数是不够的,功能满足不了需求,需要自己定义、定制构造函数
对象字面量是键值对,所以是:,自定义函数是代码语句是=号,赋值号。
this指向的是谁,是对象,不是构造函数。对象是new出来的。
没有new出来对象,在构造函数Teacher中name、sex都不存在,因为不是var name = ‘张三’; 不是声明语句,在AO中name、sex都不存在。
构造函数实例化对象。

  1. function Teacher() {
  2. this.name = '张三';
  3. this.sex = '男';
  4. this.weight = 130;
  5. this.smoke = function () {
  6. this.weight--;
  7. console.log(this.weight);
  8. }
  9. this.eat = function () {
  10. this.weight++;
  11. console.log(this.weight);
  12. }
  13. }
  14. var t1 = new Teacher();
  15. var t2 = new Teacher();
  16. t2.name = '李四';
  17. t1.smoke(); // 129
  18. t1.smoke(); // 128
  19. t2.smoke(); // 129
  20. console.log(t1)//Teacher:object
  21. console.log(new Teacher().sex)//男
  22. console.log(Teacher)//func Teacher
  23. console.log(Teacher.name)//Teacher
  24. console.log(Teacher.sex)//undefined

三、实例化

  1. function Teacher() {
  2. this.name = "张三";
  3. this.sex = "男";
  4. this.weight = 130;
  5. this.smoke = function () {
  6. this.weight--;
  7. console.log(this.weight);
  8. };
  9. this.eat = function () {
  10. this.weight++;
  11. console.log(this.weight);
  12. };
  13. }
  14. var teacher1=new Teacher();
  15. var teacher2=new Teacher();
  16. teacher1.name='李四';
  17. console.log(teacher1,teacher2)

image.png
teacher1、teacher2是不同的对象,new出来的对象都是完全不同的,新的对象
改变一个对象对另一个对象没有影响

  1. //初始化构造函数
  2. function Teacher(name, sex, weight, course) {
  3. this.name = name;
  4. this.sex = sex;
  5. this.weight = weight;
  6. this.course = course;
  7. }
  8. var t1 = new Teacher('李四', '女', 150, 'HTML');
  9. console.log(t1);

实例化对象时就给其属性赋值,不用实例化之后在赋值

  1. function Teacher(opt) {
  2. this.name = opt.name;
  3. this.sex = opt.sex;
  4. this.weight = opt.weight;
  5. this.course = opt.course;
  6. this.smoke = function () {
  7. this.weight--;
  8. console.log(this.weight);
  9. }
  10. this.eat = function () {
  11. this.weight++;
  12. console.log(this.weight);
  13. }
  14. }
  15. var t1 = new Teacher({
  16. name: '张三',
  17. sex: '男',
  18. weight: 145,
  19. course: 'JS'
  20. })

传一整个对象比传多个参数好
多个参数不知道参数的具体意义,对顺序有要求
vue.js就是这么写的,配置的

四、作业

写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加和相乘的功能

  1. function Compute() {
  2. var length = arguments.length;
  3. var sum = 0;
  4. var mul = 1;
  5. for (var i = 0; i < length; i++) {
  6. sum += arguments[i];
  7. mul *= arguments[i];
  8. }
  9. this.add = function () {
  10. console.log(sum);
  11. }
  12. this.mul = function () {
  13. console.log(mul);
  14. }
  15. }
  16. var c1 = new Compute(1, 2, 3, 4);
  17. c1.add();
  18. c1.mul();
  19. console.log(nn.length)//undefined

不用this,用var声明,外部不能调用

  1. function Compute(){
  2. var args=arguments;//保存Compute函数的arguments,供别的函数使用,要不add的arguments是从ys.add(1,2,3,4)传入的
  3. this.add=function() {
  4. sum=0;
  5. for (var i = 0; i <args.length; i++){
  6. sum += args[i];
  7. }
  8. console.log(sum)
  9. }
  10. this.mul=function() {
  11. var mul=1;
  12. for (var i = 0; i <args.length; i++){
  13. mul*=args[i]
  14. }
  15. console.log(mul)
  16. }
  17. }
  18. var ys=new Compute(1,2,3,4)
  19. ys.add()
  20. ys.mul()

mul也需要循环一遍

  1. function Compute(){
  2. var args=arguments;
  3. this.add=function() {
  4. // sum=0;
  5. // for (var i = 0; i <args.length; i++){
  6. // sum += args[i];
  7. // }
  8. // console.log(sum)
  9. console.log(ys(args,"+",0))
  10. }
  11. this.mul=function() {
  12. // var mul=1;
  13. // for (var i = 0; i <args.length; i++){
  14. // mul*=args[i]
  15. // }
  16. // console.log(mul)
  17. console.log(ys(args,"*",1))
  18. }
  19. function ys(args,symbol,res) {
  20. for (var i = 0; i < args.length; i++){
  21. if(symbol=='+'){
  22. res+=args[i]
  23. }
  24. else if (symbol =='*'){
  25. res *= args[i]
  26. }
  27. }
  28. return res;
  29. }
  30. }
  31. var ys=new Compute(1,2,3,4)
  32. ys.add()
  33. ys.mul()
  34. ys.add()

写一个构造车的函数,可设置车的品牌,颜色,排量;再写一个构造消费者的函数,设置用户的名字,年龄,收入,通过选择的方法实例化该用户喜欢的车,再设置车的属性

  1. function Car(opt) {
  2. this.brand = opt.brand;
  3. this.color = opt.color;
  4. this.displacement = opt.displacement;
  5. }
  6. function Person(opt) {
  7. this.name = opt.name;
  8. this.age = opt.age;
  9. this.income = opt.income;
  10. this.caropt = opt.caropt;
  11. this.select = function (caropt) {
  12. return new Car(caropt)
  13. }
  14. this.mycar = this.select(this.caropt);
  15. }
  16. var p1 = new Person({
  17. name: '张三',
  18. age: 25,
  19. income: '15K',
  20. caropt: {
  21. brand: '奔驰',
  22. color: 'red',
  23. displacement: 100
  24. }
  25. });
  26. console.log(p1.mycar);
  1. function Car(opt){
  2. this.brand=opt.brand ;
  3. this.color=opt.color;
  4. this.delivery=opt.delivery;
  5. }
  6. function Consumer(opt){
  7. this.name=opt.name;
  8. this.age=opt.age;
  9. this.income=opt.income;
  10. this.selectCar = function(caropt) {
  11. return new Car(caropt)
  12. };
  13. }
  1. function Car(opt){
  2. this.brand=opt.brand ;
  3. this.color=opt.color;
  4. this.delivery=opt.delivery;
  5. }
  6. function Consumer(opt){
  7. this.name=opt.name;
  8. this.age=opt.age;
  9. this.income=opt.income;
  10. // this.caropt=opt.caropt;
  11. // this.selectCar = function(caropt) {
  12. // return new Car(caropt)
  13. // };
  14. // this.myCar=this.selectCar(this.caropt)
  15. this.selectCar=function() {
  16. return new Car(opt.caropt);
  17. }
  18. }
  19. var consumer=new Consumer({
  20. name:'enjoy',
  21. age:18,
  22. income:10000,
  23. caropt: {
  24. brand:'benz',
  25. color:'red',
  26. delivery:2.0
  27. }
  28. })
  29. console.log(consumer.selectCar())
  1. function Car(opt){
  2. this.brand=opt.brand ;
  3. this.color=opt.color;
  4. this.delivery=opt.delivery;
  5. }
  6. function Consumer(opt){
  7. this.name=opt.name;
  8. this.age=opt.age;
  9. this.income=opt.income;
  10. this.caropt=opt.caropt;
  11. this.selectCar = function(caropt) {
  12. return new Car(caropt)
  13. };
  14. this.myCar=this.selectCar(this.caropt)
  15. }
  16. var consumer=new Consumer({
  17. name:'enjoy',
  18. age:18,
  19. income:10000,
  20. caropt: {
  21. brand:'benz',
  22. color:'red',
  23. delivery:2.0
  24. }
  25. })
  26. console.log(consumer)