First, create the object
1.1 Initializer
var any={name:"Some", age:ten, action:function() { alert ( this. name+ ":" +this. age);};
1.2 Construction methods
function Some (name,age) {this. name=name; this. age= age; this. action=function() { alert (the. name+ ":" +this. ); }} var New Some ("BB", 5); Aa.action ();
1.3 Properties
function C () { this. Objpro = "Object Properties"; C.prototype.objpro2= "Object attribute 2"; var privateobj = "Private attribute"; = "Class attribute";
1.4 Object Methods
function C () {varfunction() {alert ("Private Method");} This function () {alert ("Object Method");} This function () {alert ("Object method"function() {Alert ("class method");}
Second, the package
var any={name:"Some", age:ten, action:function() { alert ( this. name+ ":" +this. age);};
Iii. inheritance
3.1Dog or dog?
function Animal (name,age) { }function Dog (name,age) { animal.apply (this , [Name,age]);}
3.2Dog becomes animal.
function Animal (name,age) { }function Dog (name,age) { animal.apply (this, [ Name,age]);}
Dog.prototype=new Animal ();
Iv. polymorphic
functionAnimal (name,age) { This. say=function(){}}functionDog (name,age) { This. say=function() {}}dog.prototype=NewAnimal ();functionCat (name,age) { This. say=function() {}}cat.prototype=NewAnimal ();functionsay () {if(AnimalinstanceofAnimal) {Animal.say ();}}
JavaScript Advanced Article-Object-oriented features