简体   繁体   English

“function xy()”和“xy = function()”之间有什么区别?

[英]What's the difference between “function x.y()” and “x.y = function ()”?

The only difference between these two pieces of code are in line 1 in both blockquotes. 这两段代码之间的唯一区别在于两个块引用中的第1行。

Why do these two pieces of code behave different? 为什么这两段代码表现不同?

function cat.meow() {
    console.log("Meow");
};

versus

cat.meow = function () {
    console.log("Meow");
};

The first one is invalid JavaScript, the second is a better idea, more details in the following... 第一个是无效的JavaScript,第二个是更好的主意,下面有更多细节......

Why? 为什么?

Variable / Function names can only have Letters, Underscores, $, Numbers (if they're not the first character) along with a few other ACII, and unicode characters (check here or here as recommended by Kaiido and Felix King ). 变量/函数名称只能包含Letters,Underscores,$,Numbers(如果它们不是第一个字符)以及其他一些ACII和unicode字符 (请按照此处此处按照KaiidoFelix King的建议进行检查 )。 It would be the same as var cat.meow; 它与var cat.meow;相同var cat.meow; which is invalid JavaScript and will throw an error. 这是无效的JavaScript,会抛出错误。 More specifically 进一步来说

SyntaxError: Unexpected token '.'. SyntaxError:意外的标记'。'。

The compiler is expecting a parenthesis or a space but it sees a period not assuming it is an object. 编译器期望括号或空格,但它看到一个句点而不是假设它是一个对象。 This also happens with bracket syntax for an object :( which means you can't define a function for an object 'item' the first way 这也发生在对象的括号语法中:(这意味着你不能为第一种方法定义对象'item'的函数

Explanation 说明

When you do: foo = function () {} it set the value of foo to the function. 当你这样做: foo = function () {}它将foo的值设置为函数。 When you do it as a function function foo () {} it 'creates' foo . 当你将它作为函数function foo () {}它'创建' foo

So... 所以...

The second one is referring to meow in cat . 第二个是指cat meow So it is setting meow in cat to the function which is valid. 因此,它是设置meowcat到有效的功能。 That is equivalent to this: 这相当于:

var cat = {
    meow: function () {
        console.log("Meow!");
    }
};

Obviously is cat isn't defined, this will still throw an error. 显然是cat没有定义,这仍然会抛出一个错误。 If you really really want to have your function to be called cat.meow (I would strongly advise against it) , you would do the following 如果你真的想拥有你的函数被调用cat.meow(我会强烈建议反对),你会做以下

window['cat.meow'] = function () {
    console.log("Meow");
};

This clutters the global scope unless you have a different object and is extremely bad practice; 这会使全局范围变得混乱,除非你有一个不同的对象并且是非常糟糕的做法; so if you do use it, do it in secret otherwise almost every programmer will be bashing you on your coding practices. 因此,如果您使用它,请秘密进行,否则几乎每个程序员都会抨击您的编码实践。

Have you at least tried it? 你至少尝试过吗? The first one isn't a valid javascript. 第一个不是有效的JavaScript。 It should produce 它应该产生

Uncaught SyntaxError: Unexpected token . 未捕获的SyntaxError:意外的令牌。

The second one is valid. 第二个是有效的。 In that context, cat is an object and you're creating a meow method for it. 在这种情况下, cat是一个对象,你正在为它创建一个meow方法。

The error suggests what that . 错误暗示了什么. is not a valid identifier . 不是有效的标识符 In javascript, and most languages, anything which matches the regex \\w+ , meaning alphanumerical characters + underscore, is a valid identifier. 在javascript和大多数语言中,任何与正则表达式\\w+匹配的内容,即字母数字字符+下划线,都是有效的标识符。

And in most languages, including javascript, it is also mandatory to have the identifier begin with an alphabet or underscore. 大多数语言中,包括javascript, 标识符也必须以字母或下划线开头。 In javascript however, $ is also a valid identifier. 但是在javascript中, $也是一个有效的标识符。

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

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