JavaScript原型模式

function Campus(location, name) {
    this.location = location;
    this.name = name;
}

Campus.prototype.information = function() {
    console.log(this.location + ', ' + this.name);
}

var aaa = new Campus('广州', '中山大学');
var bbb = new Campus('武汉', '武汉大学');
console.log(aaa === bbb);
console.log(aaa.information === bbb.information);
aaa.information();
bbb.information();

/**
 * @description
 * 解决了工厂模式遗留的类型问题和成员方法共享问题
 * 也解决了构造函数模式遗留的成员方法共享问题
 * 同时也解决了全局方法的污染问题
 * 综合来看,原型模式比较完美
 */

猜你喜欢

转载自blog.csdn.net/qq_23143555/article/details/81162596