javascript:inheritance(2)

原型 prototypal

基于原型的继承相比基于类得继承在概念上更为简单:一个新对象可以继承一个旧对象的属性。

if(typeof Object.beget !== 'function')
{
Object.beget
= function(o)
{
var F= function(){};
F.prototype
= o;
return new F();
};
}
var myMammal = {
name:
'Herb the Mammal',
get_name:
function(){return this.name;},
says:
function(){return this.saying || '';}
};

var myCat = Object.beget(myMammal);
myCat.name
= 'Hemmal';
myCat.saying
= 'meow';
myCat.purr
=function(n)
{
var i, s = '';
for(i = 0; i < n; i ++)
{
if(s)
{my
s
+= '-';
}
s
+= 'r';
}
return s;
};
myCat.get_name
= function()
{
return this.says() + ' ' + this.name + ' ' + this.says();
};

函数化 Functional

原文地址:https://www.cnblogs.com/RitaRichard/p/2112744.html