构造函数,构造函数的继承和原型链

构造函数的首字母建议大写

关键面试题,new的作用:

1.创建了一个空对象 

2.让构造函数内部的this指向了这个对象 

3.将构造属性和方法添加给这个对象 

4.返回这个对象

例:  function Person(name, age, tel) {

      this.name = name

       this.age = age

      this.mobile = tel

    }

     let person1 = new Person('zhangsan', 18, 13111111111)

    console.log(person1)

  function createObj() {

      return {

        name: 'wangwu'

      }

    }

如果对象不return输出的值就是undefined

继承:子类获取父类的属性和方法的过程

例: function Game(name, type) {

      this.name = name

      this.type = type

    }

    function GameMap(name) {

      this.mapName = name

    }

GameMap.prototype = new Game('王者荣耀', '手游')

    GameMap.prototype.constructor = GameMap

    function Role(roleName, sex, hit) {

      this.roleName = roleName

      this.sex = sex

      this.hit = hit

    }

    Role.prototype = new Game('王者荣耀', '手游')

    Role.prototype.constructor = Role

    let role = new Role('程咬金', '男', 70)

    console.log(role)

    实例化对象可以直接使用原型上的方法或者属性

    console.log(role.roleName + '是' + role.name + '中的角色')

    一个实例化对象如果要使用他的属性或者方法首先会在自身的属性中去找,如果找不到就去他的原型上找,

    如果原型上也找不到就会去原型的原型上去找 知道找到或者undefined或者报错为止 这个查找的过程就叫原型链

    let role = new Role('程咬金', '男', 70)

你可能感兴趣的:(构造函数,构造函数的继承和原型链)