该方法实现一个类型的原型方法
这个方法在类型的prototype属性上面添加一个新的方法。
语法:
myType.implement(name, method);或 myType.implement(methods);
参数:
- name - (string) 方法名.
- method - (function) 方法函数.
或
- methods - (object) 一(键:值)对象 , 键是方法名,值是方法函数。
返回:
(object) 该类型.
例子:
Array.implement('limitTop', function(top){
for (var i = 0, l = this.length; i < l; i++){
if (this[i] > top) this[i] = top;
}
return this;
});
[1, 2, 3, 4, 5, 6].limitTop(4); // returns [1, 2, 3, 4, 4, 4]
也可以传递一个对象:
String.implement({
repeat: function(times){
var string = '';
while (times--) string += this;
return string;
},
ftw: function(){
return this + ' FTW!';
}
});
'moo! '.repeat(3); // returns "moo! moo! moo! "
'MooTools'.ftw(); // returns "MooTools FTW!"
('MooTools'.ftw() + ' ').repeat(2); // returns "MooTools FTW! MooTools FTW! "