In JavaScript, a reference type is a data structure that organizes data and functionality together.
An object is an instance of a specific reference type. How to create objects:
var person = new Object();
The above example 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: constructor and object literal.
1.Constructor method
Use the new operator followed by the Object constructor.
var p = new Obejct();p.name = "Xiao Xiao Yihan";p.age = 18;
2. Object literal method
An abbreviation for object definition is to simplify the process of creating objects with a large number of attributes. Example:
var p = { name:"Xiao Xiao Yihan", age:18};In object literal syntax, property names can also use strings, for example:
var p = { "name":"Xiao Xiao Yihan", "age":18, 5:true}The above example will create an object, including three attributes: name, age, and 5. The numerical attribute names here will be automatically converted to strings.
Additionally, when using object literal syntax, if you leave its curly braces blank, you can define an object containing the default properties and methods. For example:
var p = {};p.name = "Xiao Xiao Yihan";p.age = 18;Generally speaking, accessing the properties of an object uses dot notation, and in JavaScript, square bracket notation can also be used to access the properties of an object. When using square bracket syntax, the attributes to be accessed should be placed in square brackets as strings, for example:
alert(p["name"]);alert(p.name);
There is no difference between the two access methods in terms of functionality. The advantage of square bracket syntax is that properties can be accessed through variables:
var propName = "name";alert(p[propName]); // "Xiao Xiao Yihan"
Square bracket notation can also be used if the attribute name contains keywords or reserved characters, etc. that will cause errors. For example:
p["first name"] = "Xiao Xiao Yihan";
The property first name contains a space and cannot be accessed through dot notation.
The above article in-depth analysis of JavaScript:Object type is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.