深入挖掘js之继承
发布时间:2020-12-14 04:50:57 所属栏目:大数据 来源:网络整理
导读:一、伪类 当一个函数对象被创建时,Function构造器产生的函数对象会运行类似这样的代码: this.prototype = { constructor: this}; 新函数对象被赋予了一个prototype属性,他的值是一个包含constructor属性且属性值为该新函数的对象。这个prototype对象是存
一、伪类
this.prototype = { constructor: this};
Function.method('new',function(){ //创建一个新对象,它继承自构造函数的原型对象 var that = Object.create(this.prototype); // 调用构造函数,绑定-this-到新对象上 var other = this.apply(that,arguments); //如果返回值不是一个对象,就返回该新对象 return (typeof other ==='object'&&other)||that; }) 我们定义一个构造器来扩充它的原型 var Mammal = function(name){ this.name=name; }; Mammal.prototype.get_name = function(){ return this.name; }; Mammal.prototype.says = function(){ return this.saying || ''; } 现在我们构造一个实例 var myMammal = new Mammal('hello world!'); var name = myMammal.get_name(); 我们构造一个伪类来继承Mammal,通过定义它的constructor函数并替换他的prototype为一个Mamma var Cat = function(name){ this.name =name; this.saying= 'meow'; } 替换Cat.prototype为一个新的Mammal实例 Cat.prototype =new Mammal(); 扩充新原型对象增加get_name方法 Cat.prototype.get_name = function(){ return this.says()+''+this.name+''+this.says(); }; var myCat =new Cat('wsm'); var says = myCat.says(); var name= myCat.get_name(); 二、原型
Object.create方法构造更多的实例 三、函数化运用模块模式的继承模式去保护隐私。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |