[code="javascript"]
Class.Methods = {
addMethods: function(source) {
var ancestor = this.superclass && this.superclass.prototype;
var properties = Object.keys(source);
//下面这两句是干什么用的?貌似永远都走不到啊!Object.keys({ toString: true }).length永远等于1啊!
//跑不到的代码还要写上来,不理解中。往指点!我第一次看prototype源码
if (!Object.keys({ toString: true }).length)
properties.push("toString", "valueOf");
for (var i = 0, length = properties.length; i < length; i++) {
var property = properties[i], value = source[property];
if (ancestor && Object.isFunction(value) &&
value.argumentNames().first() == "$super") {
var method = value;
value = (function(m) {
return function() { return ancestor[m].apply(this, arguments) };
})(property).wrap(method);
value.valueOf = method.valueOf.bind(method);
value.toString = method.toString.bind(method);
}
this.prototype[property] = value;
}
return this;
}
};
[/code]
Object.keys({ toString: true }).length这个不一定是1哦,
万一有人用了delete把它删除了,这句就有用了