There are two ways to create an object instance
① using the new operator with the object constructor
var person = new Object ();p erson.name = "Tom";p eison.age = 25;
② literal: The feeling of being encapsulated
var person = { name: ' Tom ', //' name ' = ' Tom ' age:25 //' age ' = 25};
Property names can also use strings.
The literal passes a large number of optional parameters:
function DisplayInfo (args) { var output = ""; if (typeof Args.name = = "string") { output = "Name:" + args.name + "\ n"; } if (typeof args.age = = "Number") { output = "Age:" + args.age + "\ n"; } alert (output); } DisplayInfo ({ //literal parameter name: "Tom", age:25 });
Access Object properties can be expressed in dot notation and square brackets
Alert (person["name"]); Square brackets alert (person.name); Point//variable to access the property var propertyname = "Name"; alert (Person[propertyname]);
You can also use square brackets if the property name contains a character that causes a syntax error, or if the property name uses a keyword or reserved word.
5.1 Object Type