In programming languages, literals are notation methods that represent values. For example, "Hello, World!" represents a string literal in many languages, and JavaScript is no exception. The following are examples of JavaScript literals, such as 5, true, false and null, which represent an integer, two boolean values and an empty object, respectively.
JavaScript also supports object and array literals, allowing for the creation of arrays and objects using a concise and readable notation. Consider the following statement where an object (firstName and lastName) is created with two properties:
You can also create the same object using an equivalent method:
To the right of the above assignment statement is an object literal. An object literal is a list of name-value pairs, each name-value pair is separated by commas and enclosed in a brace. Each name value pair represents an attribute of the object, and the two parts of the name and value are separated by a colon. To create an array, you can create an instance of the Array object:
However, the preferred method is to use an array literal, which is a comma-separated list of values enclosed in brackets:
The previous example shows that object and array literals can contain other literals. Here is a more complex example:
The object assigned to the team variable has 3 properties: name, members, and count. Note that '' represents an empty string, [] is an empty array. Even the value of the count property is a literal, that is, the function literal:
The function literal is constructed as follows: the function keyword is preceded by a function name (optional) and parameter table. Then there is the function body, surrounded by braces.
The above has introduced literals. Let’s introduce JavaScript object notation (JSON), which is a notation used to describe files and arrays, consisting of a subset of JavaScript literals. JSON is becoming more and more popular among Ajax developers because this format can be used to exchange data, often replacing XML.
====================================================================================================
Example of JavaScript Object Literals (Original)
Object literal:
//Only add static properties and methods var myObject={ propertyA: sha , propertyB: feng , methodA:function(){ alert(this.propertyA+ +this.propertyB); }, methodB:function(){}}myObject.methodA();//Use the prototype attribute to add public properties and method function myConstructor2(){}; //Declare the constructor, you can use object literal syntax to add all public members to the prototype attribute myConstructor2.prototype={ propertyA: sha , propertyB: feng , methodA:function(){ alert(this.propertyA+ +this.propertyB); }, methodB:function(){}}var myconstructor=new myConstructor2(); //Declare the object myconstructor.methodA();The above is the literal explanation of JavaScript objects. I hope it can give you a reference and I hope you can support Wulin.com more.