1. Object Tool Encapsulation
2. Factory mode
function Createperson (name,age,job) {var o = {name:name,age:age,job:job, sayname:function () {alert (this.name);}}; return o; };var Tanya = Createperson ("Tanya", "a", "female"); var Ansel = Createperson ("Ansel", "a", "male"); Tanya.sayname (); Ansel.sayname ();?
3. constructor Mode function Mathfunc (A, b) {this.a = a;this.b = B;this.add = function () {return a+b;}} var add = new Mathfunc (3,4), var sum = Add.add (), alert (sum);
4.? apply andcall?function Mathfunc (A, b) {this.a = a;this.b = B;this.add = function () {return a+b;}} var myadd={};//mathfunc.call (myadd,31,4); Mathfunc.apply (myadd,[31,4]); var sum = Myadd.add (); alert (sum);
5. Prototype Sharing methodfunction Mathfunc (b) {this.a = a;this.b = b; MathFunc.prototype.add = function () {return a+b;}}; var myadd= new Mathfunc (3,4), var sum = Myadd.add (), alert (sum);
JS Create object Practice