First, define the basic format of the object:
var hero = {breed: ' Turtle ', Occupation: ' Ninja ', talk:function () { alert ("a!!"); } Author: { firstname: ' Martin ', LastName: ' Lu ' }}
Property names are not quoted, but they need to be quoted when the property name has reserved words or special characters (spaces, etc.)
Second, the operation of the object properties
You can add/remove properties or methods for an object at any time:
var hero = {};hero.breed = ' turtle '; hero.name = ' Alice '; hero.sayname = function () {return hero.name;}
In an object, this refers to this object, such as the following method of invocation:
var hero = {name: ' Martin ', sayname:function () { return this.name;}} Hero.sayname ();
Third, use the constructor function to create the object
You can accept some parameters when you create an object, and use the New keyword to create an object
function Hero (name) {this.name = Name;this.occupation = ' Ninja '; this.whoareyou = function () { return "I am" + this.name +this.occupation;}} var hero = new Hero (' Martin ');
Four, constructor properties
When an object is created, the object automatically generates a constructor property that points to a reference to the constructor function that is used to create the object.
Like Hero.constructor, the return is Hero (name)
You can use this property to create another new object, as shown in the following example:
var hero1 = new Hero.constructor (' Martin ');
Note that the above code can be used to create new objects in this way even if the hero object was not previously created.
If the object was created by using text notation, it is created by the built-in function constructor, object (), as follows:
var o = {};
O.constructor;
>>>object ()
typeof O.constructor;
>>> "function"
V. Create an object using a function that returns an object
You can create an object using a function that returns an object:
function Factory (name) {return { name:name;}} var o = Factory ("Martin"); o.name; return to Martino.constructot; Return Object ()
This problem occurs when a function that returns an object creates an object with new: The object created by new (returned by the constructor) is no longer a factory object, but an object that contains the Name property
JavaScript Basics Blind Spot Summary--object