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
