设计模式系列-2.构造函数模式

设计模式系列-2.构造函数模式

强制执行构造函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var Car = function(name, price){
if(!(this instanceof Car)){ // 防止无限循环
return new Car(name, price);
}
this.name = name;
this.price = price;
}

Car.prototype.getInfo = function(){
return this.name + ': ' + this.price;
}

console.log(new Car('voe', 23333);.getInfo());
console.log(Car('ssss', 12312).getInfo());
其实这真的是JS中无处不在的设计模式
1
2
3
var s = new String('Hello world');
var n = new Number(1);
var b = new Boolean(true);

延伸考题 new String() 和直接定义真的一样吗

包装函数可以加属性进去

1
2
s.var_1 = 'var a';
console.log(s.var_1);

原始类型不行, 怎么定义都是undefined

1
2
3
var string = 'abc';
string.aa = 'bcd';
console.log(string.aa); // undefined