js中的構造函數
聲明構造函數
function Demo(args) {
this.a = args.a;
this.b = args.b;
}
構造函數中的this表示新創建的對象,構造函數自動返回this
構造函數的原型對象,能夠被所有實例繼承
Demo.prototype.fn = function() {
console.log(this.a + this.b);
}
let demo = new Demo({a: "aa", b: "bb"});
demo.fn(); // aabb
console.log(demo.constructor) // [Function: Demo]
console.log(demo instanceof Demo); // true
