用于标记匿名函数的Javascript“冒号”? [英] Javascript 'colon' for labeling anonymous functions?
问题描述
这段代码也提到了什么?
queryString:function(){
/ / some code
}
我在WebConsole(Firefox)但它不会执行,所以我认为它不等于函数queryString(){}
。
那究竟是什么?
您在那里缺少一些代码,但我认为它是对象的一部分像这样的声明:
pre $ var bj $ {
queryString:function(){
// some code
}
};
obj.queryString();
它将函数指定为对象文字的属性。它相当于这样:
var obj = {};
obj.queryString = function(){...};
obj.queryString();
一般而言,对象字面值语法如下所示:
{key:value,otherKey:otherValue};
所以这在控制台中不起作用的原因是它没有被包含在 {}
字符,表示对象字面值。而且这个语法只在对象文本中有效。
What does this code refer too?
queryString: function() {
//some code
}
I tested it in the WebConsole (Firefox) but it wouldn't execute, so I'm thinking that it isn't equivalent to function queryString() {}
.
So what is it exactly?
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屋!