![](/img/trans.png)
[英]why does it say .setDescription is not a function? (RichEmbed in JavaScript)
[英]Why does it say xxx is not a function
为什么这样不行?
aContract = function(){};
aContract.prototype = {
someFunction: function() {
alert('yo');
},
someOtherFunction: some$Other$Function
};
var some$Other$Function = function() {
alert('Yo yo yo');
};
var c = new aContract();
c.someFunction();
c.someOtherFunction();
Firebug说c.someOtherFunction不是函数
但这很好
aContract = function(){};
aContract.prototype = {
someFunction: function() {
alert('yo');
},
someOtherFunction: some$Other$Function
};
function some$Other$Function() {
alert('Yo yo yo');
};
var c = new aContract();
c.someFunction();
c.someOtherFunction();
我在这里想念什么??? 我更喜欢使用第一种方法在javascript中进行编码,该方法通常可以正常工作,但在我制作原型时似乎无法正常工作。
谢谢,〜ck in桑迪·艾格
在评估时:
aContract.prototype = { ... }
尚未评估:
var some$Other$Function = function() { ... }
因此, aContract.prototype.someOtherFunction
设置为undefined
。
第二个起作用的原因是因为函数声明 (第二个是第一个是表达式)在任何其他语句之前被求值。 这里有更多详细信息: 神秘的命名函数表达式
在实际创建some$Other$Function
之前,已将some$Other$Function
分配给aContract.prototype.someOtherFunction
。 语句的顺序很重要。 如果您切换事物的顺序,那么您会很好:
var some$Other$Function = function() {
alert('Yo yo yo');
};
aContract = function(){};
aContract.prototype = {
someFunction: function() {
alert('yo');
},
someOtherFunction: some$Other$Function
};
这是由于吊装。 函数语句移至其作用域的顶部。
编辑:验证Crockford的JavaScript:The Good Parts页面113。
看起来,在全局作用域中, var
工作原理与在函数局部作用域中的工作原理不同。 在局部函数范围内,将var
声明的变量提升到函数的顶部(即,在函数中使用关键字var
声明的任何变量在该函数中的其他任何地方都可用)。 但在全球范围内,也许只有使用关键字的function
来声明一个变量产生同样的结果(即用关键字声明的变量function
将提供全球范围内的任何地方,即使在声明前行)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.