用于标记匿名函数的Javascript“冒号"? [英] Javascript 'colon' for labeling anonymous functions?
问题描述
这段代码也指的是什么?
What does this code refer too?
queryString: function() {
//some code
}
我在 WebConsole (Firefox) 中对其进行了测试,但它无法执行,因此我认为它不等同于 function queryString() {}
.
I tested it in the WebConsole (Firefox) but it wouldn't execute, so I'm thinking that it isn't equivalent to function queryString() {}
.
那究竟是什么?
推荐答案
你在那里遗漏了一些代码,但我认为它是这样的对象声明的一部分:
You are missing some code there, but I assume its part of an object declaration like this:
var obj = {
queryString: function() {
//some code
}
};
obj.queryString();
它将函数分配为对象字面量的属性.这相当于:
It assigns a function as a property of an object literal. It would be equivalent to this:
var obj = {};
obj.queryString = function() { ... };
obj.queryString();
通常,对象字面量语法如下所示:
In general, the object literal syntax looks like this:
{ key: value, otherKey: otherValue };
所以这在控制台中不起作用的原因是它没有包含在 {}
字符中,表示对象文字.并且此语法仅在对象字面量中有效.
So the reason this didn't work in the console is that it was not enclosed in {}
characters, denoting an object literal. And this syntax is valid ONLY in an object literal.
这篇关于用于标记匿名函数的Javascript“冒号"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!