1. Factory mode
Ex
function Createperson (name, age, Job) {var o = new Object (); o.name = Name;o.job = job; o.sayname = function () {alert (th Is.name);}; return o;} var person1 = Createperson ("Nicholas", "Software Engineer"), var person2 = Createperson ("Greg", "Doctor");
Creates an object by returning an internal object;
2. Constructor mode
Ex
function person (name, age, Job) {this.name = name; this.age = Age;this.job = Job;this.sayname = function () {alert (This.nam e);};} var person1 = new Person ("Nicholas", "Software Engineer"), var person2 = new Person ("Greg", "Doctor");
As a constructor, create a new object, assign the scope of the constructor to the new object, execute the code in the constructor, add properties and methods, and return the new object.
The downside is that the same thing that the same method function does requires a number of different or unequal functions to be produced.
3. Prototype mode
function person () {}person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function () {alert (this.name);}; var person1 = new Person ();p erson1.sayname (), var person2 = new Person ();p erson2.sayname (); alert (Person1.sayname = = Person2.sayname); /true
All instances share the properties and methods of the prototype
The prototype object understands:
Prototype's constructor property points to the new build function
The prototype property of the build function points to the prototype prototype
New object's [[Prototype]] property points to the prototype Prototype
Person.prototype.isPrototypeOf (Person1)//true
Person.prototype.isPrototypeOf (Person2)//true
Object.getprototypeof (person1) = = Person.prototype
Object.getprototypeof (person1). Name//access
Here the access first accesses whether the object has this property, if none, and then accesses the prototype to have the attribute, and then access the higher-level prototype, and so on.
hasOwnProperty can detect whether a property exists in an instance or in a prototype, and this method returns true only if the given property exists in the object instance.
JS Elevation Note--Create object