首页 > 网站 > WEB开发 > 正文

javascript设计模式之构造函数模式

2024-04-27 14:19:19
字体:
来源:转载
供稿:网友

javascript设计模式之构造函数模式

// 构造函数模式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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表