// 构造函数模式function Car(name, logo) { // 强制使用new, "this" point to the instance if (!(this instanceof Car)) { return new Car(name, logo); } this.name = name; this.logo = logo;}// Car.PRototype is the original object, that every instance pointed to.Car.prototype.output = function () { return (this.name + " : " + this.logo);}// new Car("King", "horse");var mycar = Car("King", "horse"); // this is equal "new Car('King', 'horse')"console.log(mycar.output()); // King : horseconsole.log(Car.prototype === mycar.__proto__); // trueconsole.log(Car.prototype.constructor === Car); // true
新闻热点
疑难解答