Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript syntax

Tags:

javascript

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.
         }
like image 307
Tom Avatar asked Sep 09 '11 14:09

Tom


People also ask

What syntax does JavaScript use?

JavaScript uses the Unicode character set.

What is '$' in JavaScript?

$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).

How do I write code in JavaScript?

To write a JavaScript, you need a web browser and either a text editor or an HTML editor. Once you have the software in place, you can begin writing JavaScript code. To add JavaScript code to an HTML file, create or open an HTML file with your text/HTML editor.

Is JavaScript syntax hard to learn?

Arguably, JavaScript is one of the easiest programming languages to learn, so it serves as a great first language for anyone brand new to coding. Even the most complex lines of JavaScript code can be written one by one, in fragments. It can also be tested in the web browser at the same time.


1 Answers

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());
like image 91
Alex Turpin Avatar answered Oct 19 '22 01:10

Alex Turpin