我有一个问题,了解一些JavaScript语法如下:条件语句/语法混乱
var myObject = {
value: 0;
increment: function (inc) {
this.value += typeof inc === 'number' ? inc : 1;
}
};
myObject.increment();
document.writeln(myObject.value); // 1
myObject.increment(2);
document.writeln(myObject.value); // 3
具体做法是:
this.value += typeof inc === 'number' ? inc : 1;
是该行称,如果:
typeof inc === 'number'
则:
this.value += inc
什么好办法去想这个或资源来帮助理解,将不胜感激。
这如果“inc”是一个有效的数字,那么使用它来增加,否则使用1.你也可以写:this.value + =(isNaN(inc)?1:inc); – 2012-01-11 02:54:45