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.
}
As some of the other answers here indicate, it is an example of object literal notation. An object can be declared like so:
However, it can also be declared with object literal notation, like so:
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:
“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):
In this example, however, this refers to the enclosing object, myObj: