1 functionPerson (name, age) {2 This. Name =name;3 This. Age =Age ;4 This. Eat =function() {5Alert ("Eating ...");6 }7 }8 varP1 =NewPerson ("cai10", 21);9 varP2 =NewPerson ("cai20", 27);Tenalert (p1.name);//cai10 Onealert (p1.age);// + AP1.eat ();//Eating ... -Alerttypeofp1);//Object -Alert (P1instanceofObject);//true theAlert (P1instanceofperson);//true -alert (p1.constructor);//function person (name, age) {...} -Alert (p1.eat = = p2.eat);//false
Creates and returns a new object by using the New keyword.
In person (), no created objects are displayed,
Instead, the properties and methods are assigned directly to the this object, and there is no return statement.
You create an instance by using the constructor pattern, which identifies the instance as a specific type,
In the example, the instanceof operator or the constructor property can be used for validation.
However, there is still a problem with multiple function instances.
JavaScript Creation Object--constructor mode