Writing this topic is simply to take notes for yourself, or old forget.
The first method:
function fn1 (x) {this.x = x; } function fn2 (x, y) {this.tmpobj = fn1; This.tmpobj (x); Delete this.tmpobj; This.y = y; }
Second method: Call () or apply ()
function fn1 (x) {this.x = x; } function fn2 (x, y) {Fn1.call (this, x); This.y = y; }
Third method: Prototype chain inheritance
function fn1 (x) {this.x = x; } fn1.prototype.y = function () {Console.log ("I Am pomelo"); } function Fn2 () {} Fn2.prototype = new fn1 (); Fn2.prototype.constructor = fn2; var fn2obj = new Fn2 (); Fn2obj.y ();
The most used is the second and the third type.
function fn1 (x) {this.x = x; } fn1.prototype.z = function () {Console.log ("I Am pomelo"); } function fn2 (x, y) {fn1.apply (this, [x]); This.y = y; } Fn2.prototype = new fn1 (); Fn2.prototype.constructor = fn2; var fn2obj = new Fn2 (1024, 2048); Console.log (fn2obj.x); Console.log (FN2OBJ.Y); Fn2obj.z ();
Methods for JavaScript Object inheritance