码迷,mamicode.com
首页 > 编程语言 > 详细

语言精粹【摘要】

时间:2014-10-31 15:22:34      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   for   sp   on   cti   bs   line   

if (typeof Object.beget !== ‘function‘) {

Object.beget = function (o) {
var F = function () {};
F.prototype = o;
return new F();
};
}
var another_stooge = Object.beget(stooge);

 

Function.prototype.method = function (name, func) {
if (!this.prototype[name]) {
this.prototype[name] = func;
}
};

Object.method(‘superior‘, function (name) {
var that = this,
method = that[name];
return function ( ) {
return method.apply(that, arguments);
};
});

var mammal = function (spec) {
var that = {};

that.get_name = function ( ) {
return spec.name;
};

that.says = function ( ) {
return spec.saying || ‘‘;
};

return that;
};

var myMammal = mammal({name: ‘Herb‘});

var cat = function (spec) {
spec.saying = spec.saying || ‘meow‘;
var that = mammal(spec);
that.purr = function (n) {
var i, s = ‘‘;
for (i = 0; i < n; i += 1) {
if (s) {
s += ‘-‘;
}
s += ‘r‘;
}
return s;
};
that.get_name = function ( ) {
return that.says( ) + ‘ ‘ + spec.name +
‘ ‘ + that.says( );
}
return that;
};
var myCat = cat({name: ‘Henrietta‘});
var coolcat = function (spec) {
var that = cat(spec),
super_get_name = that.superior(‘get_name‘);
that.get_name = function (n) {
return ‘like ‘ + super_get_name( ) + ‘ baby‘;
};
return that;
};

var myCoolCat = coolcat({name: ‘Bix‘});
var name = myCoolCat.get_name( );
// ‘like meow Bix meow baby‘

 

语言精粹【摘要】

标签:style   io   ar   for   sp   on   cti   bs   line   

原文地址:http://www.cnblogs.com/dingyuanxin/p/4064991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!