对象

  • 对象中的this 永远指向对象本身。
  • 对象字面量,对象直接量

    1. var teacher = {
    2. name: '张三',
    3. age: 20,
    4. height: 177,
    5. weight:120,
    6. sex: 'male',
    7. teach: function () {
    8. console.log('i am teaching JavaScript');
    9. },
    10. smoke: function () {
    11. this.weight--;
    12. console.log(this.weight);
    13. },
    14. eat: function () {
    15. this.weight++;
    16. console.log(this.weight);
    17. }
    18. }
    19. //这个teaacher 就是对象字面量。
    1. var attendance = {
    2. students: [],
    3. total:6,
    4. join: function (name) {
    5. var idx = this.students.indexOf(name);
    6. if (idx!==-1) {
    7. console.log(name + '已经到了');
    8. return
    9. }
    10. if (this.students.length === 6) {
    11. console.log('已到齐');
    12. return;
    13. }
    14. this.students.push(name);
    15. },
    16. leave: function (name) {
    17. var idx = this.students.indexOf(name);
    18. if (idx !== -1) {
    19. console.log(name + '早退');
    20. this.students.splice(idx, 1);
    21. }
    22. },
    23. classOver: function () {
    24. this.students = [];
    25. }
    26. }

    构造函数

    1. var obj = {};//对象字面量
    2. var obj = new Object();//对象构造函数。
    3. //以上两种方式创建的对象完全相同。

    自定义构造函数

  • 首字母都是大写。大驼峰。

  • 构造器中的this 如果构造器没有实例化,就是没有意义。
  • 构造器中的this在构造函数被实例化之后才有用。
    1. function Teacher(opt) {
    2. this.name = opt.name;
    3. this.age = opt.age;
    4. this.sex = opt.sex;
    5. this.weight = weight;
    6. this.smoke = function () {
    7. this.weight--;
    8. }
    9. this.eat = function () {
    10. this.weight++;
    11. }
    12. }
    13. var teacherOne = new Teacher({
    14. name: '张三',
    15. age: 20,
    16. sex: 'male',
    17. weight:'130'
    18. })
    19. var teacherTwo = new Teacher({
    20. name: '李四',
    21. age: 21,
    22. sex: 'female',
    23. weight:'100'
    24. })

    demo

  1. 写一个构造函数,接受函数类型的参数,参数不定,完成参数相乘或相加。
  2. 写一个构造函数。可以设置车的品牌,颜色,排量。再写一个消费者的函数,设置用户的名字。年龄,收入。通过选车的方式。实例化改用户喜欢的车。再设置车的属性。
    1. function Count() {
    2. var args = arguments;//实参
    3. this.count = function () {
    4. let num = 0;
    5. for (let i = 0; i < args.length; i++) {
    6. let val = args[i];
    7. //不是number 类型就直接跳过
    8. if (typeof (val) != 'number') {
    9. continue
    10. }
    11. num += val;
    12. }
    13. console.log(num)
    14. }
    15. console.log(arguments);
    16. }
    17. var c = new Count(1, 2, 3, 4, 6);
    18. c.count();
    ```javascript

function Car(opt) { this.brand = opt.brand; this.color = opt.color; this.output = opt.output; } function Person(opt) { this.name = opt.name; this.age = opt.age; this.income = opt.income; this.selectCar = function () { var car = new Car(opt.carOpt) console.log(this.name + ‘ s favoriate car is ‘ + car.brand); } } var personA = new Person({ name: ‘PA’, age: 20, income: 20000, carOpt:{ brand: ‘BMW’, color: ‘pink’, output:’2.0T’ } }) var personB = new Person({ name: ‘PA’, age: 20, income: 10000, carOpt:{ brand: ‘Benz’, color: ‘black’, output:’1.8T’ } })

```