寻找原始值JS

时间:2017-03-20 21:58:35

标签: javascript

如何创建一个接收一个参数的函数,并在控制台中打印该参数的基础值,无论是基本类型还是函数?

value = function(args){ 
    return args.valueOf() //Only working for line 36
};

var scary = 'boo';
var first = function() { return 'bar'; };
var second = function() {
    return first;
};

var third = function() {
    return second;
};

var nested = function() {
  return function() {
    return function() {
      return function() {
        return function() {
          return function() {
            return function() {
              return function() {
                return function() {
                  return 'super nested';
                };
              };
            };
          };
        };
      };
    };
  };
};


// expected values for all these assessments
console.log(value(scary));   // should be      'boo'
console.log(value(first));   // should be      'bar'
console.log(value(second));  // should also be 'bar'
console.log(value(third));   // should also be 'bar'
console.log(value(nested));  // should be      'super nested'

我在这里制作的值函数只给了我第一行可怕而只有其余的函数,我如何得到其他函数的值?

2 个答案:

答案 0 :(得分:5)

您可以检查收到的args是否有https://github.com/nodejs/node-addon-examples/blob/master/3_callbacks/nan/addon.cc,如果是,请重新使用您的value函数以及您的论证结果

如果没有,则返回值

var value = function(args){ 
    if (args && args.apply) {
      return value( args() );
    }
    return args;
};

var scary = 'boo';
var first = function() { return 'bar'; };
var second = function() {
    return first;
};

var third = function() {
    return second;
};

var nested = function() {
  return function() {
    return function() {
      return function() {
        return function() {
          return function() {
            return function() {
              return function() {
                return function() {
                  return 'super nested';
                };
              };
            };
          };
        };
      };
    };
  };
};


// expected values for all these assessments
console.log(value(scary));   // should be      'boo'
console.log(value(first));   // should be      'bar'
console.log(value(second));  // should also be 'bar'
console.log(value(third));   // should also be 'bar'
console.log(value(nested));  // should be      'super nested'

或者您可以使用与提及的.apply类似的方法来查看它是否为函数,我最常使用here功能。

正如Ibu在评论中提到的,如果你需要这样的值函数,你可能需要检查你的代码设计。它可以更加优化:)

答案 1 :(得分:3)

虽然已经回答,但另一种解决方案是检查typeof args

var value = function(args){ 
    if (typeof args === 'function') {
        return value(args.call());
    }
    return args.valueOf() //Only working for line 36
};

var scary = 'boo';
var first = function() { return 'bar'; };
var second = function() {
    return first;
};

var third = function() {
    return second;
};

var nested = function() {
  return function() {
    return function() {
      return function() {
        return function() {
          return function() {
            return function() {
              return function() {
                return function() {
                  return 'super nested';
                };
              };
            };
          };
        };
      };
    };
  };
};


// expected values for all these assessments
console.log(value(scary));   // should be      'boo'
console.log(value(first));   // should be      'bar'
console.log(value(second));  // should also be 'bar'
console.log(value(third));   // should also be 'bar'
console.log(value(nested));  // should be      'super nested'