In JavaScript, a reference type is a data structure that is used to organize data and functionality together.
An object is an instance of a specific reference type. How objects are created:
var person = new Object ();
The example above creates a new instance of the object reference type, and then saves the instance in the variable person.
There are two ways to create objects that have constructors and object literals.
1. constructor function mode
Use the new operator followed by the object constructor.
var p = new obejct ();
P.name = "The Rustling and the cold";
P.age = 18;
2. Object Literal method
A shorthand for defining objects to simplify the process of creating objects that contain a large number of properties. Example:
var p = {
name: "The Rustle of the cold",
age:18
};
Property names can also use strings when object literal syntax is used, for example:
var p = {
"name": "The Rustle of the cold",
"age":
5:true
}
The example above creates an object that contains name, age, 53 attributes. The numeric property name here is automatically converted to a string.
In addition, when you use object literal syntax, you can define an object that contains default properties and methods if you leave the curly braces blank. For example:
var p = {};
P.name = "The Rustle is cold";
p.age = 18;
In general, access to the properties of an object is a dot notation, and in JavaScript you can access the object's properties using the square brackets notation. When you use the bracket syntax, you should place the property you want to access in square brackets as a string, for example:
Alert (p["name"]);
alert (p.name);
There is no difference in functionality between these two ways of accessing. The advantage of the bracket syntax is that you can access the property through a variable:
var propname = "name";
Alert (P[propname]); "The rustling and the cold"
You can also use the square brackets notation if the property name contains keywords or reserved words that result in incorrect characters. For example:
p["the" "Name" = "The Rustle of the cold";
Property-Name contains a space that cannot be accessed by dot notation.
The above in-depth analysis of the Javascript:object type is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.