构造函数

1. es6方案
    Object.create 根据指定的原型创建新对象,原型可以是 null; 
    Object.getPrototypeOf 获得一个对象的原型; 
    Object.setPrototypeOf 设置一个对象的原型

    Object.create(proto, [propertiesObject])
    语法使用, proto 是对象原型, propertiesObject可有可无

2.es5方案
通过new的方式去创建
eg 
2.1 构造器中添加属性
    function c1() {
        this.p1 = '测试1'
    this.p2=function() {
            console.log(this.p1)
        }
    }
    var o1 = new c1
    o1.p2()
    涉及到一个this的指向问题, 这个里的this 指向函数或者说是函数的调用者
2.2 构造器的 prototype 属性上添加属性
    function c2() {
        c2.prototype.p1 =1
        c2.prototype.p2 = function () {
            console.log(this.p1)
        }
    }
    var o2 = new c2
    o2.p2()

2.3 class关键字
    class Proto {
        dis() {
            this.width = width
            this.height = height
        }
        pp () {
            return this.width + this.height
        }
    }
    let ccc = new Proto(3, 9)
    console.log(ccc.pp())
    注意,new 的时候就要传入参数,要不然会是NAN
    extends 提供了继承能力
    eg:下面例子中的test1继承了 rectangle基本属性
    class test1 extends rectangle {
        constructor(height, width) {
            super(width, height)
        }
        say() {
            return this.width + this.height
        }
    }
    var bbb = new test1(3, 6)
    console.log(bbb.say())
class 关键字和箭头运算符可以完全替代旧的 function 关键字
原文地址:https://www.cnblogs.com/ralapgao/p/10823753.html