ES6 —— 构造函数和原型


1、利用构造函数创建对象
  1. 构造函数是一种特殊的函数,主要用来初始化对象,即为对象成员变量附初始值,它总与 new 一起使用。我们可以把对象中一些公共属性和方法抽取出来,然后封装到这个函数里面。
  2. new 在执行时会做的四件事情:
  1. 在内存中创建一个新的空对象。
  2. 让 this 指向这个新的对象。
  3. 执行构造函数里面的代码,给这个新对象添加属性和方法。
  4. 返回这个新对象(所以构造函数里面不需要 return)
	// 利用构造函数创建对象
    function Star(uname, age){
        this.uname = uname
        this.age = age
        this.sing = function() {
            console.log('I can sing')
        }
    }
    let a = new Star('毛不易', 20)
    console.log(a) //Star {uname: '毛不易', age: 20, sing: ƒ}
    a.sing() //I can sing
2、实例成员和静态成员
  1. 实例成员:构造函数内部通过 this 添加的成员。只能通过实例化的对象来访问。
  2. 静态成员:在构造函数本身上添加的成员。只能通过构造函数来访问。
	// 构造函数中的属性和方法我们称为成员 成员可以添加
    function Star(uname, age){
        this.uname = uname
        this.age = age
        this.sing = function() {
            console.log('I can sing');
        }
    }
    let a = new Star('毛不易', 20)
    // 1. 实例成员就是构造函数内部通过this添加的成员 uname age sing 就是实例成员
    // 实例成员只能通过实例化的对象来访问
    console.log(a.uname) //毛不易
    a.sing() //I can sing

    // console.log(Star.uname) //undefined  不可以通过构造函数来访问实例成员

    // 2.静态成员 在构造函数本身上添加的成员 sex 就是静态成员
    Star.sex = '男'
    // 静态成员只能通过构造函数来访问
    console.log(Star.sex) //男
    // console.log(a.sex) //undefined 不能通过对象来访问
3、构造函数原型对象 prototype
  1. 构造函数的问题:存在浪费内存的问题。
	function Star(uname, age){
        this.uname = uname
        this.age = age
        this.sing = function() {
            console.log('I can sing');
        }
    }
    let a = new Star('毛不易', 20)
    let b = new Star('水木年华', 25)
    console.log(a.sing === b.sing) //false 因为会创建两个函数
  1. 构造函数原型 prototype:构造函数通过原型分配的函数是所有对象所 共享的
  1. JavaScript 规定,每一个构造函数都有一个 prototype 属性,指向另一个对象。注意这个 prototype 就是一个对象,这个对象的所有属性和方法,都会被构造函数所拥有。
  2. 我们可以把那些不变的方法,直接定义在 prototype 对象上,这样所有的实例就可以共享这些方法。
  3. 原型是什么?一个对象,我们也成为 prototype 为 原型对象。
  4. 原型的作用:共享方法
	function Star(uname, age){
        this.uname = uname
        this.age = age
    }
    Star.prototype.sing = function(){
        console.log('I can sing')
    }
    let a = new Star('毛不易', 20)
    let b = new Star('水木年华', 25)
    console.log(a.sing === b.sing)

    a.sing() //I can sing
    b.sing() //I can sing
4、对象原型 __proto__
  1. 对象都会有一个属性 __proto__ 指向构造函数的 prototype 原型对象,之所以我们对象可以使用构造函数 prototype 原型对象的属性和方法,就是因为对象有 __proto__ 原型的存在。
  2. __proto__ 对象原型和原型对象 prototype 是等价的
  3. __proto__ 对象原型的意义在于为对象的查找机制提供一个方向,或者说一条线路,但是它是一个非标准属性,因此在实际开发中,不可以使用和这个属性,它只是内部指向原型对象 prototype。
	function Star(uname, age){
        this.uname = uname
        this.age = age
    }
    Star.prototype.sing = function(){
        console.log('I can sing');
    }
    let a = new Star('毛不易', 20)
    let b = new Star('水木年华', 25)
    a.sing() //I can sing
    console.log(a) //Star {uname: '毛不易', age: 20} 对象身上系统自己添加一个 __proto__ 指向我们构造函数的原型对象
    console.log(a.__proto__ === Star.prototype) //true
    // 方法查找规则:
    // 如果a 对象上有sing 方法 就执行这个对象上的 sing
    // 如果没有sing 这个方法 因为有__proto__的存在 就去构造函数原型对象 prototype 身上去查找sing 这个方法
5、constructor 构造函数
  1. 对象原型(__proto__)和构造函数(prototype)原型对象里面都有一个constructor 属性,constructor 我们称为构造函数,因为它指回构造函数本身。
  2. constructor 主要用于记录该对象引用于哪个构造函数,它可以让原型对象重新指向原来的构造函数。
  3. 如果我们修改了原来的原型对象,给原型对象赋值的是一个对象,则需要手动的利用 constructor 这个属性指回原来的构造函数。
	function Star(uname, age){
        this.uname = uname
        this.age = age
    }
    // 如果我们修改了原来的原型对象 给原型对象赋值的是一个对象 则需要手动的利用 constructor 这个属性指回 原来的构造函数
    Star.prototype = {
        constructor: Star,
        sing(){
            console.log('I can sing')
        },
        movie(){
            console.log('I can perform in a film')
        }
    }
    let a = new Star('毛不易', 20)
    let b = new Star('水木年华', 25)
    console.log(Star.prototype)
    console.log(a.__proto__)
    console.log(Star.prototype === a.__proto__)
    console.log(Star.prototype.constructor)
    console.log(a.__proto__.constructor)

在这里插入图片描述

如果不在 Star 原型对象上添加:constructor: Star,就不会有构造函数。

在这里插入图片描述

  1. 构造函数、实例、原型对象三者之间的关系
    在这里插入图片描述
6、原型链
  1. 只要有对象就有 __proto__ 原型。
    在这里插入图片描述
	function Star(uname, age){
        this.uname = uname
        this.age = age
    }
    Star.prototype.sing = function(){
        console.log('I can sing')
    }
    let a = new Star('毛不易', 20)
    console.log(a.__proto__ === Star.prototype) //true
        
    // 1.只要有对象就有 __proto__ 原型 指向原型对象
    console.log(a.__proto__) //指向Star.prototype
        
    // 2.我们Star原型对象里面的__proto__原型指向的是 Object.prototype
    console.log(Star.prototype.__proto__) //指向Object.prototype

    // 3.我们Object.prototype 原型对象里面的__proto__原型 指向为 null
    console.log(Object.prototype.__proto__) //指向 null

在这里插入图片描述

7、JavaScript 成员的查找机制(规则)
  1. 一层一层的查找,如果多层都含有某个属性,会就近选择。

第一层

	function Star(uname, age){
        this.uname = uname
        this.age = age
    }
    Star.prototype.sing = function(){
        console.log('I can sing')
    }
    // Star.prototype.sex = '女'
    // Object.prototype.sex = '不详'
    let a = new Star('张三', 20)
    a.sex = '男'
    console.log(a.sex) //男

第二层

	function Star(uname, age){
        this.uname = uname
        this.age = age
    }
    Star.prototype.sing = function(){
        console.log('I can sing')
    }
    Star.prototype.sex = '女'
    // Object.prototype.sex = '不详'
    let a = new Star('张三', 20)
    // a.sex = '男'
    console.log(a.sex) //女

第三层

	function Star(uname, age){
        this.uname = uname
        this.age = age
    }
    Star.prototype.sing = function(){
        console.log('I can sing')
    }
    // Star.prototype.sex = '女'
    Object.prototype.sex = '不详'
    let a = new Star('张三', 20)
    // a.sex = '男'
    console.log(a.sex) //不详

多层用最近的一层

	function Star(uname, age){
        this.uname = uname
        this.age = age
    }
    Star.prototype.sing = function(){
        console.log('I can sing')
    }
    Star.prototype.sex = '女'
    Object.prototype.sex = '不详'
    let a = new Star('张三', 20)
    a.sex = '男'
    console.log(a.sex) //男
8、原型对象中的 this 指向
  1. 构造函数里面 this 指向的是对象实例。
  2. 原型对象函数里面的 this 指向的是实例对象。
	function Star(uname, age){
        this.uname = uname
        this.age = age
    }
    let that
    Star.prototype.sing = function(){
        console.log('I can sing')
        that = this
    }
    
    let a = new Star('张三', 20)
    // 1.构造函数里面 this 指向的是对象实例a
    a.sing() //I can sing
    // 2.原型对象函数里面的this 指向的是实例对象a
    console.log(that === a) //true
9、扩展内置对象
  1. 可以通过原型对象,对原来的内置对象进行扩展自定义的方法。
	// 原型对象的应用 扩展内置对象方法
    console.log(Array.prototype)

    Array.prototype.sum = function() {
        let sum = 0
        for(let i = 0; i < this.length; i++){
            sum += this[i]
        }
        return sum
    }
    let arr = [1, 2, 3]
    console.log(arr.sum())
    console.log(Array.prototype)

    let arr1 = new Array(2, 3, 4)
    console.log(arr1.sum())

在这里插入图片描述

  1. 数组和字符串内置对象不能给原型对象覆盖操作 Array.prototype = {},只能是 Array.prototype.xxx = function() {} 的方式。
	Array.prototype = {
        sum(){
            let sum = 0
            for(let i = 0; i < this.length; i++){
                sum += this[i]
            }
            return sum
        }
    }
    let arr = [1, 2, 3]
    console.log(arr.sum())
    console.log(Array.prototype)

    let arr1 = new Array(2, 3, 4)
    console.log(arr1.sum())

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端杂货铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值