一段JS代码

allysniper 2010-01-25 11:51:46
// A simple helper that allows you to bind new functions to the
// prototype of an object
Function.prototype.method = function(name, func) {
this.prototype[name] = func;
return this;
};
// A (rather complex) function that allows you to gracefully inherit
// functions from other objects and be able to still call the 'parent'
// object's function
Function.method('inherits', function(parent) {
// Keep track of how many parent-levels deep we are
var depth = 0;
// Inherit the parent's methods
var proto = this.prototype = new parent();
// Create a new 'priveledged' function called 'uber', that when called
// executes any function that has been written over in the inheritance
this.method('uber', function uber(name) {
var func; // The function to be execute
var ret; // The return value of the function
var v = parent.prototype; // The parent's prototype
// If we're already within another 'uber' function
if (depth) {
// Go the necessary depth to find the orignal prototype
for ( var i = d; i > 0; i += 1 ) {
v = v.constructor.prototype;
}
// and get the function from that prototype
func = v[name];
CHAPTER 3 n CREATING REUSABLE CODE 41
// Otherwise, this is the first 'uber' call
} else {
// Get the function to execute from the prototype
func = proto[name];
// If the function was a part of this prototype
if ( func == this[name] ) {
// Go to the parent's prototype instead
func = v[name];
}
}
// Keep track of how 'deep' we are in the inheritance stack
depth += 1;
// Call the function to execute with all the arguments but the first
// (which holds the name of the function that we're executing)
ret = func.apply(this, Array.prototype.slice.apply(arguments, [1]));
// Reset the stack depth
depth -= 1;
// Return the return value of the execute function
return ret;
});
return this;
});
// A function for inheriting only a couple functions from a parent object,
// not every function using new parent()
Function.method('swiss', function(parent) {
// Go through all of the methods to inherit
for (var i = 1; i < arguments.length; i += 1) {
// The name of the method to import
var name = arguments[i];
// Import the method into this object's prototype
this.prototype[name] = parent.prototype[name];
}
return this;
});


求// A (rather complex) function that allows you to gracefully inherit
// functions from other objects and be able to still call the 'parent'
// object's function 以后代码的详细解释,特别是
for ( var i = d; i > 0; i += 1 ) {
v = v.constructor.prototype;
}
这是什么意思,无限循环?
还有ret = func.apply(this, Array.prototype.slice.apply(arguments, [1]));其中Array.prototype.slice.apply(arguments, [1])这是什么意思
...全文
124 11 打赏 收藏 举报
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
NewHandRookie 2010-01-31
  • 打赏
  • 举报
回复
+534364643QQ 我详细说
allysniper 2010-01-27
  • 打赏
  • 举报
回复
....
qq373591361 2010-01-26
  • 打赏
  • 举报
回复
jf
yixianggao 2010-01-26
  • 打赏
  • 举报
回复
// Go the necessary depth to find the orignal prototype 
for ( var i = d; i > 0; i += 1 ) {
v = v.constructor.prototype;
}

向上循环查找原始原型,不过确实没有终止条件,执行时应该会抛异常滴!
allysniper 2010-01-26
  • 打赏
  • 举报
回复
自已顶个...
yb121331 2010-01-25
  • 打赏
  • 举报
回复
这在干吗呢
allysniper 2010-01-25
  • 打赏
  • 举报
回复
怎么没有人解释一下啊...
jumpheightway 2010-01-25
  • 打赏
  • 举报
回复
这是javascript面向对象编程
拷贝对象属性
fengyunshier 2010-01-25
  • 打赏
  • 举报
回复
for ( var i = d; i > 0; i += 1 ) {
v = v.constructor.prototype;
}
这个除了无限循环应该不能做其他的了吧,
看不懂。。。
netajax 2010-01-25
  • 打赏
  • 举报
回复
UP了
netajax 2010-01-25
  • 打赏
  • 举报
回复
// 注释了的吗?

我不是很懂。。。。。。。。

UP下吧.......
发帖
JavaScript

8.7w+

社区成员

Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
帖子事件
创建了帖子
2010-01-25 11:51
社区公告
暂无公告