1. Create an object through JSON
2. New object first, and then add attributes and Methods. Disadvantages: a large number of repeated codes will be generated.
var people = new OBject();people.name = "aaaa";people.getName = function(){ alert('get the name:' +this.name);}
3. Object literal. disadvantage: a large number of repeated codes are generated.
var people = { name: "aaaa", getName: function(){ alert('get the name:'+this.name); }}
4. Factory mode. disadvantage: the object type cannot be identified.
function createFunction(name){ var o = new Object(); o.name = name; o.getName = function(){ alert('get the name: '+this.name); }
return o;}
5. constructor mode. Disadvantages: Method sharing is not allowed.
function People(name){ this.name = name; this.getName = function(){ alert('get the name '+this.name); }}
6. prototype mode. Disadvantages: Some attributes do not need to be shared.
var Peopel = {}People.prototype = { constructor:People, name:"aaa", getName:function(){ alert('the name is:' +this.name); }}
7. Combined use of the constructor mode and prototype mode
function People(name){ this.name = name;}People.prototype = { constructor:People; getName: function(){ alert('the name is'+this.name); }}
8. Dynamic Prototype mode. This is a better object creation mode.
1 function People(name){2 this.name = name;3 if(typeof this.getName != "function"){4 People.prototype.getName = function(){5 alert('the name is:' +this.name);6 }7 }8 }