1 /*2 JS implementation of Object-oriented method3 */4 5 //1 Factory model not recommended6 functionPerson (name, sex, age) {7obj = {};8Obj.name =name;9Obj.sex =sex;TenObj.age =Age ; OneObj.say =function(){ AAlert This. Name + "" + This. sex+ "" + This. age); - }; - returnobj; the } - - varP1 = Person ("Z3", "female", 18); - P1.say (); + - + A at //2 recommended constructor Methods - //the first letter to capitalize when creating a class this is a specification - functionPerson (name, sex, age) { - This. Name =name; - This. Sex =sex; - This. Age =Age ; in This. Say =function(){ -Alert This. Name + "" + This. sex+ "" + This. age); to }; + } - varP2 =NewPerson ("Z3", "male", 18); the //P2.say (); * varP3 =NewPerson ("Z4", "female", 20); $ //P3.say ();Panax Notoginseng - the //Obj.constructor is the constructor of obj +Alert (p2.constructor== person);//true A the //verifies that an instance is not an object of an instance instanceof +Alert (P2instanceofperson);//true -Alert (P2instanceofObject);//true JS in which everything is Object $ $ - - the //3 Deprecated, binding methods use call or Apply - functionPerson (name, sex, age) {Wuyi This. Name =name; the This. Sex =sex; - This. Age =Age ; Wu This. Say =function(){ -Alert This. Name + "" + This. sex+ "" + This. age); About }; $ } - varn \NewObject (); - //use call to bind an object to O to bind a Person object instance, which is the argument after O -Person.call (O, "Z3", "male", 19); A O.say (); +
JavaScript implements object-oriented