【发布时间】:2011-10-19 13:05:06
【问题描述】:
我正在阅读 crockford 的 Javascript:The Good Parts,并且正在处理课程调用模式中的这段代码:
var br = "<br />";
var add = function(a,b) {
a + b;
}
var myObject = {
value: 0,
increment: function(inc) {
this.value += typeof inc === "number" ? inc : 1;
}
};
myObject.increment(2);
document.write(myObject.value + br); // 2
myObject.increment();
document.write(myObject.value + br); // 3
myObject.increment(3);
document.write(myObject.value + br); // 5
myObject.double = function() {
var that = this;
var helper = function() {
that.value = add(that.value,that.value);
return that.value;
};
helper();
};
myObject.double();
document.write(myObject.value); //undefined
在调用double 方法后,我得到了undefined。有谁知道为什么?
【问题讨论】:
-
我认为这不是问题的原因,但不建议在 JavaScript 中使用保留字作为标识符。
double是保留字。 -
@Jacob:50% 正确:允许在 Javascript 中使用特定数据类型的计划不再存在,因此 double 不再在最新标准的保留字列表中。由于许多人仍在使用将其定义为保留字的早期浏览器,因此您应该避免使用它。
标签: javascript oop this