liuzhang_850607 2011-11-24 10:13
浏览 252
已采纳

一段代码的疑问

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];

        // 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;
});



这段代码的作用,这个for循环是不是有问题
  for ( var i = d; i > 0; i += 1 ) {
                v = v.constructor.prototype;
            }

  • 写回答

5条回答 默认 最新

  • kendy_2007 2011-11-24 11:15
    关注

    for ( var i = d; i > 0; i += 1 ) {

    v = v.constructor.prototype;

    }

    循环的作用是将v指向最初的原型。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序