admin管理员组

文章数量:1425899

I keep finding some JavaScript that looks like the example below. Can someone explain this as I have not seen JavaScript written like this before.

What is "SomethingHere" and the colon represent? I'm used to seeing function myFunction() but not what is shown below.

 SomethingHere: function () {

              There is code here that I understand.
         }

I keep finding some JavaScript that looks like the example below. Can someone explain this as I have not seen JavaScript written like this before.

What is "SomethingHere" and the colon represent? I'm used to seeing function myFunction() but not what is shown below.

 SomethingHere: function () {

              There is code here that I understand.
         }
Share Improve this question edited Sep 9, 2011 at 14:56 Daniel A. White 191k49 gold badges379 silver badges466 bronze badges asked Sep 9, 2011 at 14:55 TomTom 612 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 7

It's object literal notation. It's a way of declaring objects using the following form:

{
    propertyName: value
}

So for example:

var obj = {
    fruit: "apple",
    id: 123,
    sayHello: function() { return "Hi"; }
};
alert(obj.fruit);
alert(obj.id);
alert(obj.sayHello());

Basically in that scope/syntax, you have to have named arguments and this is the way to get the function into the object.

You can create an object in Javascript like this:

var obj = {
 a: 1,
 b: 2
};

You can also have attributes which are functions:

var obj = {
 SomethingHere: function() { alert("hello"); },
 b: 2
};

As some of the other answers here indicate, it is an example of object literal notation. An object can be declared like so:

var myObj = new Object();

However, it can also be declared with object literal notation, like so:

var myObj = { };

When using object literal syntax, one can immediately add methods and properties inside the open and closing braces using the syntax name : value. For example:

var myObj = {
    name: 'Dave',
    id: 42,
    SomethingHere: function() {
        /* function body */
    }
};
alert(myObj.name); // Displays Dave
alert(myObj.id);   // Displays 42
myObj.SomethingHere(); // Executes method SomethingHere

"SomethingHere," in this case is a "method," meaning that it is a function that is a member of an object. It's significance lies in the special variable this. In the following two function definitions, this refers to the browser window variable (assuming that are running the code in a browser):

function foo() {
    /* this.document refers to window.document */
    /* this.location refers to window.location*/
}

var bar = function() {
    /* Same as above, just a different syntax for declaring the function.
    /* this.document refers to window.document */
    /* this.location refers to window.location*/
}

In this example, however, this refers to the enclosing object, myObj:

var myObj = {
    name = 'Dave',
    id = 42,

    myMethod: function() {
        /* this.name refers to the name property of myObj, i.e., 'Dave' */
        /* this.id refers to the id property of myObj, i.e., 42 */
        /* this.location is undefined */
        /* this.document is undefined */
    }
};

EDITED: No reference to JSON, as it can be misunderstood with the data format.

That's JavaScript object notation. You can make a function like:


var SomethingHere = function() {

};

or...


var Singleton = {
   SomethingHere: function()
   {
    ...
   }
}

本文标签: JavaScript syntaxStack Overflow