4

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.
         }

5 Answers 5

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());
Sign up to request clarification or add additional context in comments.

5 Comments

more precisely: object literal notation.
...also your alert(obj.sayHello()) is incorrect as sayHello uses alert() itself.
Sure enough. Don't hesitate to edit the post on occurences like this.
OK -- I tend to avoid editing other people's answers, as it almost always seems heavy-handed to me.
I suppose it depends on the people. Well, if you ever come accross one of my posts again, edit away :)
2

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

Comments

2

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
};

Comments

1

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 */
    }
};

Comments

0

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

10 Comments

That is technically not JSON. It is just a JavaScript object. JSON is a data format based on JavaScript objects but its syntax is a bit more strict. For example, key names must be enclosed in quotes. See more here: json.org
It isn't JSON. JSON is a data format based on JavaScript's syntax for object literals, array literals and various other basic data types. It does not include functions.
Bottom line is that "SomethingHere" is a variable which is defined to be a function. You can use "=" to define a function outside of a JavaScript object or by using ':" to make it a property of an object.
@Chris, thank you! I did my reading, and naturally, you're right, it isn't technically JSON.
If you want to reach for even more accuracy, you could mention that it is object literal notation, as opposed to using constructor/instance/prototype objects: davidpirek.com/… Enjoy the reading :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.