Javascript“冒号”标记匿名函数?

时间:2022-05-31 22:29:23

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() {}.

我在WebConsole (Firefox)中测试了它,但是它不会执行,所以我认为它并不等同于函数queryString(){}。

So what is it exactly?

那么到底是什么呢?

5 个解决方案

#1


64  

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.

因此,这在控制台中不起作用的原因是它没有被包含在{}字符中,表示对象的文字。这个语法只在对象文本中有效。

#2


10  

This is probably inside a map/object declaration like so:

这可能是在地图/对象声明中,比如:

var obj = {
    queryString: function() {
        alert('here');
    },
    eggs: function() {
        alert('another function');
    }
};

obj.queryString();

#3


5  

It's a label https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

这是一个标签https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

var i, j;

loop1:
for (i = 0; i < 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j < 3; j++) {   //The second for statement is labeled "loop2"
      if (i == 1 && j == 1) {
         continue loop1;
      }
      console.log("i = " + i + ", j = " + j);
   }
}

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
//   "i = 2, j = 0"
//   "i = 2, j = 1"
//   "i = 2, j = 2"
// Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"

#4


5  

The : is used when defining an object and its properties.

在定义对象及其属性时使用。

var obj = {
   queryString: function() {
      //some code
   }
}

Now obj.queryString is your function.

现在obj。变量的名称是你的函数。

#5


1  

What the

什么

queryString: function() {

//some code

}

means is the you can use queryString() to call the function that it refers to. This kind referencing is generally used if you want to define a class(or a pseudo class ;P) in your javascript. Something like this,

意味着您可以使用queryString()调用它引用的函数。如果您想在javascript中定义类(或伪类;P),则通常使用这种引用。这样的东西,

var application= { namespace: {} };

application.namespace.class_name = function(){

  function constructor(){
   return {
     exposed_property1 : property1,
     exposed_property2 : property2,  
     ...
     ...
    }
   }
  //Write property/functions that you want to expose.
  // Write rest of the function that you want private as function private(){}
};

So now at anyother part of the code you can create objects for class_name and use it to access the property1,property2 etc.,

现在在代码的其他部分你可以为class_name创建对象并使用它来访问property1,property2等等,

#1


64  

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.

因此,这在控制台中不起作用的原因是它没有被包含在{}字符中,表示对象的文字。这个语法只在对象文本中有效。

#2


10  

This is probably inside a map/object declaration like so:

这可能是在地图/对象声明中,比如:

var obj = {
    queryString: function() {
        alert('here');
    },
    eggs: function() {
        alert('another function');
    }
};

obj.queryString();

#3


5  

It's a label https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

这是一个标签https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

var i, j;

loop1:
for (i = 0; i < 3; i++) {      //The first for statement is labeled "loop1"
   loop2:
   for (j = 0; j < 3; j++) {   //The second for statement is labeled "loop2"
      if (i == 1 && j == 1) {
         continue loop1;
      }
      console.log("i = " + i + ", j = " + j);
   }
}

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
//   "i = 2, j = 0"
//   "i = 2, j = 1"
//   "i = 2, j = 2"
// Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"

#4


5  

The : is used when defining an object and its properties.

在定义对象及其属性时使用。

var obj = {
   queryString: function() {
      //some code
   }
}

Now obj.queryString is your function.

现在obj。变量的名称是你的函数。

#5


1  

What the

什么

queryString: function() {

//some code

}

means is the you can use queryString() to call the function that it refers to. This kind referencing is generally used if you want to define a class(or a pseudo class ;P) in your javascript. Something like this,

意味着您可以使用queryString()调用它引用的函数。如果您想在javascript中定义类(或伪类;P),则通常使用这种引用。这样的东西,

var application= { namespace: {} };

application.namespace.class_name = function(){

  function constructor(){
   return {
     exposed_property1 : property1,
     exposed_property2 : property2,  
     ...
     ...
    }
   }
  //Write property/functions that you want to expose.
  // Write rest of the function that you want private as function private(){}
};

So now at anyother part of the code you can create objects for class_name and use it to access the property1,property2 etc.,

现在在代码的其他部分你可以为class_name创建对象并使用它来访问property1,property2等等,