用于标记匿名函数的Javascript“冒号”? [英] Javascript 'colon' for labeling anonymous functions?

查看:167
本文介绍了用于标记匿名函数的Javascript“冒号”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码也提到了什么?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆