简体   繁体   English

为什么说xxx不是函数

[英]Why does it say xxx is not a function

why is this not ok? 为什么这样不行?

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 says c.someOtherFunction is not a function Firebug说c.someOtherFunction不是函数

But this works just fine 但这很好

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();

What am I missing here??? 我在这里想念什么??? I prefer to code in javascript using the first method, which usually works fine, but doesn't seem to work correctly when I prototype. 我更喜欢使用第一种方法在javascript中进行编码,该方法通常可以正常工作,但在我制作原型时似乎无法正常工作。

Thanks, ~ck in Sandy Eggo 谢谢,〜ck in桑迪·艾格

At the time this is evaluated: 在评估时:

 aContract.prototype = { ... }

this has not yet been evaluated: 尚未评估:

var some$Other$Function = function() { ... }

Thus aContract.prototype.someOtherFunction is set to undefined . 因此, aContract.prototype.someOtherFunction设置为undefined

The reason the second works is because function declarations (which the second is, the first is an expression) are evaluated before any other statements. 第二个起作用的原因是因为函数声明 (第二个是第一个是表达式)在任何其他语句之前被求值。 There are more details here: Named function expressions demystified 这里有更多详细信息: 神秘的命名函数表达式

You have assigned some$Other$Function to aContract.prototype.someOtherFunction before you actually create some$Other$Function . 在实际创建some$Other$Function之前,已将some$Other$Function分配给aContract.prototype.someOtherFunction The order of statements matters. 语句的顺序很重要。 If you switch the order of things you'll be good: 如果您切换事物的顺序,那么您会很好:

var some$Other$Function = function() {
    alert('Yo yo yo');
};

aContract = function(){};
aContract.prototype = {
    someFunction: function() {
        alert('yo');
    },
    someOtherFunction: some$Other$Function
};

It's due to hoisting. 这是由于吊装。 Function statements are moved to the top of their scope. 函数语句移至其作用域的顶部。

Edit: Verification Crockford's JavaScript: The Good Parts page 113. 编辑:验证Crockford的JavaScript:The Good Parts页面113。

It appears that, in the global scope, var works differently than it does in function-local scopes. 看起来,在全局作用域中, var工作原理与在函数局部作用域中的工作原理不同。 In function-local scopes, var -declared variables are lifted to the top of the function (ie, any variable declared anywhere within a function with the keyword var is available anywhere else within that function). 在局部函数范围内,将var声明的变量提升到函数的顶部(即,在函数中使用关键字var声明的任何变量在该函数中的其他任何地方都可用)。 But in the global scope, perhaps only use of the keyword function to declare a variable yields the same result (ie, that a variable declared with the keyword function will be available anywhere within that global scope, even in lines preceding that declaration). 但在全球范围内,也许只有使用关键字的function来声明一个变量产生同样的结果(即用关键字声明的变量function将提供全球范围内的任何地方,即使在声明前行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM