options未定义。options.name如果options不存在,则无法访问。
如果您想检查的不仅仅是一个属性,我建议您这样做:
var Plan = function(options){
// Set defaults
this.name = 'foo';
this.title = 'bar';
this.something = 'even more stuff';
if(options){ // If options exists, override defaults
this.name = options.name || this.name;
this.title = options.title || this.title;
this.something = options.something || this.something;
}
}
否则,我会试试这个:
var Plan = function(options){
this.name = options ? options.name || 'small' : `small`;
}
这有点难看,但你必须检查是否options存在,如果options有一个name属性。
这是做什么的:
if(options){
if(options.name){
this.name = options.name;
} else {
this.name = 'small';
}
} else {
this.name = 'small';
}