首页 > 编程语言 > 详细

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

时间:2014-09-10 15:36:00      阅读:136      评论:0      收藏:0      [点我收藏+]
// 构造函数模式
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 : horse


console.log(Car.prototype === mycar.__proto__);  // true
console.log(Car.prototype.constructor === Car);  // true

 

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

原文:http://www.cnblogs.com/xiankui/p/3964383.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!