To make the definition more consistent with the Java requirements, place the prototype code of the definition method in the person's constructor.
functionPerson (name,age,friends) {//property is defined in the constructor This. Name =name; This. Age =Age ; This. Friends =friends; //cannot be defined using overrides /*Person.prototype = {Constructor:person,//Method defined in prototype Say:function () {A Lert (this.name+ "[" +this.friends+ "]"); } }*/ /** * To determine if a Person.prototype.say exists, if it does not exist, it means to create a * when it is present, it will not create a*/ if (! Person.prototype.say) {//alert ("Init"), which is printed only oncePerson.prototype.say =function() {alert ( This. name+ "[" + This. friends+ "]"); } }}varP1 =NewPerson ("Leon", 23,["Ada", "Chris"]);p 1.name= "John";p 1.friends.push ("Mike."); P1.say (); //John ["Ada", "Chris", "Mike"]varP2 =NewPerson ("Ada", 33,["Leon"]);p 2.say ();//Ada Leon
Dynamic prototyping of JavaScript object creation