js原型补充

js定义函数:

<script>
    function A() {}
    let a1 = new A();
    let a2 = new A();
    // 为A类添加原型 => 类似于类属性
    A.prototype.num = 100;
    console.log(a1.num);
    console.log(a2.num);
</script>

ES6语法下类

class B {
    constructor(name) {
        this.name = name;
    }
}
let b1 = new B('Tom');
let b2 = new B('Ben');
B.prototype.count = 666;
console.log(b1.count);
console.log(b2.count);

console.log(b1.name);
console.log(b2.name);


posted @ 2019-12-19 19:59  MiaoQinHong  阅读(86)  评论(0编辑  收藏  举报