标签:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <script> /** * 第一种继承 */ function Persion(name) { this.name = name ? name : ‘persion‘; }; Persion.prototype.run = function() { console.log(this.name) }; function Xiaoming(name) { Persion.call(this, name); }; Xiaoming.prototype.run = function() { console.log(this.name) }; var xm = new Xiaoming(‘xiaoming‘); // xm.run(); // console.log(xm.constructor) /** * 第二种继承 */ function Persion2(name) { this.name = name ? name : ‘persion2‘; }; Persion2.prototype.run = function() { console.log(this.name) }; function Xiaoming2(name) { Persion2.call(this, name); }; Xiaoming2.prototype = new Persion2(); var xm2 = new Xiaoming2(‘xiaoming2‘); // xm2.run(); // console.log(xm2.constructor) /** * 第三种继承--比较全面 */ function Persion3(name) { this.name = name ? name : ‘persion3‘; }; Persion3.prototype.run = function() { console.log(this.name) }; function Xiaoming3(name) { //步骤 1 Persion3.call(this, name); }; //步骤 2--这时候,Xiaoming3的constructor指向Persion3,所以,需要步骤3来重置它 Xiaoming3.prototype = new Persion3(); //步骤 3 Xiaoming3.prototype.constructor = Xiaoming3; var xm3 = new Xiaoming3(‘xiaoming3‘); // xm3.run(); // console.log(xm3.constructor) </script> </body> </html>
标签:
原文地址:http://www.cnblogs.com/simonbaker/p/4566877.html