首页 > 代码库 > javascript 继承 constructor 需要注意点
javascript 继承 constructor 需要注意点
<!DOCTYPE html><html><head> <title></title></head><body></body><script> var People = function(){ this.name = "Jackey"; }; People.prototype.getName = function(){ return this.name; }; var Man = function(){ this.sex = "male"; People.call(this); }; //缺点 ,name 是父类中的属性,如果修改会影响子类 //Man.prototype = People.prototype; //new 新开内存 Man.prototype = new People(); Man.prototype.constructor = Man;//可随意修改People的属性,不会影响到Man var man = new Man(); console.log(man.getName());</script></html>
<!DOCTYPE html><html><head> <title></title></head><body></body><script> var People = function(){ this.name = "Jackey"; }; People.prototype.getName = function(){ return this.name; }; var Man = function(){ this.sex = "male"; People.call(this); }; //缺点 ,name 是父类中的属性,如果修改会影响子类 //Man.prototype = People.prototype; //new 新开内存 Man.prototype = new People(); Man.prototype.constructor = Man;//可随意修改People的属性,不会影响到Man var man = new Man(); console.log(man.getName());</script></html>
<!DOCTYPE html><html><head> <title></title></head><body></body><script> var People = function(){ this.name = "Jackey"; }; People.prototype.getName = function(){ return this.name; }; var Man = function(){ this.sex = "male"; People.call(this); }; //缺点 ,name 是父类中的属性,如果修改会影响子类 //Man.prototype = People.prototype; //new 新开内存 Man.prototype = new People(); Man.prototype.constructor = Man;//可随意修改People的属性,不会影响到Man var man = new Man(); console.log(man.getName());</script></html>
javascript 继承 constructor 需要注意点
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。