//传统类写法
function Demo(){
this.name = 'zf'; // 实例化对象的属性
this.fn = function(){ // 实例化对象的方法
alert('fn')
}
};
Demo.prototype.age = 18; // 实例化对象原型的属性
Demo.prototype.fn2 = function(){ // 实例化对象原型的方法
alert('fn2');
};
let a = new Demo();
console.log(a);
console.log(a);如下
![](https://img2018.cnblogs.com/blog/1201394/201902/1201394-20190214004942257-1636648376.png)
// ECMA6写法
class Demo{
constructor(){
this.name = 'zf'; // 实例化对象的属性
this.fn = function(){ // 实例化对象的方法
alert('fn')
}
};
fn2(){ // 实例化对象原型的方法
alert('fn2');
};
static fn3(){
alert('fn3'); // 类私有的属性 实例化对象无法获取它!
}
};
Demo.prototype.age = 18; // 实例化对象原型的属性
let a = new Demo();
console.log(a);
console.log(a);如下
![](https://img2018.cnblogs.com/blog/1201394/201902/1201394-20190214005730510-2133851058.png)
注意:
a.fn2(); // 'fn2'
//a.fn3(); // a.fn3 is not a function
Demo.fn3() // 'fn3' static 声明的属性 只有类本身才能调用!