First, JS object-oriented Programming foundation
Object is created:
function Dog (name,age) {this.name = name; /* Properties and methods for creating objects must be added this*/This.age = age; This.eat = function () {alert (this.color+, "+this.age+"), dog with the name "+this.name+" Eating Bones " }} var dog = new Dog (" Tom ", 2);d O G.eat (); /* Color undefined *//* Dynamic Add attribute method */dog.color= "BLACK"; /* Dynamically add attribute */dog.eat (); /*color for Black */delete dog.color; /* Delete attribute */dog.run=function () {/* Dynamic Add Method */alert (this.color+this.age+ "The Dog with the name" +this.name+ "is eating bones, eating and Running");} Dog.run ();
With statement : The object that needs to use its properties and methods is included with the WITH statement.
With (dog) { alert (color); alert (name);
Eat ();}
For statement : Used to traverse all properties of an object
DOG[DOG2] is the value of the dog object found through the Dog2 property */alert ("Property name:" +dog2+ "-Attribute value:" +dog[dog2]); }
Ii. Creating a custom JS Class and Object
(a) Factory mode
(Each object has its own method to occupy resources)
function Po (name) {var obj = new Object (); obj.name=name;/* sets the properties of the properties of obj instead of the PO itself to use This*/obj.say=function () { alert ("I Am" +obj.name); return obj;} var p = Po ("Zhang San"); /* Gets the return value */var p1 = Po ("John Doe"), var p = new Po ("Zhang San"),/*new is the PO object best to get */p.say () in the above way,/* Only set the properties of obj instead of the Po itself. You need to return the Obj object to invoke the method */p1.say () of obj; alert (p.say==p1.say);/*false*/
}
(ii) constructor mode (each object has its own method to occupy resources)
function Po (name) {this.name=name;this.say=function () { alert ("I Am" +this.name),}} var p = new Po ("Zhang San"); var p1 = new Po ( "John Doe");p. Say ();p 1.say (); alert (P.say==p1.say); /*false different object tune method is different that each object has its own method to occupy resources */
(iii) Prototype mode (method common but not reference)
function Po () {po.prototype.name= "John Doe"; Po.prototype.say=function () {alert ("I Am" +this.name);}} var p = new Po (), var p1 = new Po ();p. Say (); P1.say (); alert (P.say==p1.say); /*true means that all objects are in the same method *,
(iv) Construction + prototyping (solves the previously mentioned problem, but the encapsulation is not perfect)
function Po (name) {//constructor method build prototype object This.name=name;} Po.prototype.say=function () {/* prototype mode creates a common method */alert ("I Am" +this.name);} var p = new Po ("Zhang San"), var p1 = new Po ("John Doe");p. Say ();p 1.say (); alert (P.say==p1.say) ; True
(v) Better structure + prototype (a perfect solution)
function Po (name) { this.name=name;if (po.prototype.say==undefined) { po.prototype.say=function () {alert (" I am "+this.name";}}} var p = new Po ("Zhang San"), var p1 = new Po ("John Doe");p. Say ();p 1.say (); alert (P.say==p1.say) ; True
Six JSON format
var Po = {name: "Zhang San",/* comma separated, not semicolon */age:12,addr: "Chongqing", Say:function () {alert (this.age+ "old" +this.name+ "current" +this.addr);}} alert (po.name); alert (po.age); alert (PO.ADDR); Po.say ();
Third, the realization of object inheritance
(i) object posing
function Po (name) {this.name=name;this.say=function () {alert ("I Am" +this.name);}} function Falsepo (name) {THIS.FPO=PO;THIS.FPO (name);d elete This.fpo; /* You must delete the impersonated object after impersonating */this.color=function () { alert (this.name+ "Skin color is Black");}} var Falsepo = new Falsepo ("Lisi"); Falsepo.say (); Falsepo.color ();
(ii) Call ()
1 functionPo (name) {2 This. name=name;3 This. say=function(){4Alert ("I am" + This. Name);5 }6 } 7 functionFalsepo (name) {8Po.call ( This, name);//Basic class. Call (object, parameter list)9 This. color=function(){TenAlert This. name+ "The complexion is black"); One } A } - varFalsepo =NewFalsepo ("Lisi"); - Falsepo.say (); theAlsepo.color ();
(c) Apply () mode
1 functionPo (name,age) {2 This. name=name;3 This. age=Age ;4 This. say=function(){5Alert ("I am" + This. Name);6 }7 }8 9 functionFalsepo (name,age) {TenPo.apply ( This, [Name,age]);/*Basic class. Apply (object, parameter array) */ One A This. color=function(){ -Alert This. age+ "Years old" + This. name+ "The complexion is black"); - } the } - varFalsepo =NewFalsepo ("Lisi", 20); - Falsepo.say (); -Falsepo.color ();
(iv) prototype chain
JavaScript Object-Oriented programming