获取无效的形式参数

时间:2009-08-28 21:18:48

标签: javascript parameters

这是一项SO挑战

我想知道在没有arguments对象的情况下某人如何在函数中获得无效的形式参数,以模拟不知道参数解构赋值的格式。这不是ECMAScript问题,只适用于JavaScript。

您的mySolution无法访问argumentstest。您将获得一个包含参数名称的args数组。必须返回一个对象,该对象具有每个参数的属性,该参数是传递给函数的参数。简而言之,results[prop]必须=== test[prop]。您的解决方案不应该依赖于错误或安全漏洞,因为它们将来可能不存在。我想到的这个问题的解决方案并不依赖于任何错误。

(function () {
    function mySolution ({
        var,
        this,
        function,
        if,
        return,
        true
    }) {
        // prohbit reference to arguments and the test object
        var test = arguments = null,

        args = ['var', 'this', 'function', 'if', 'return', 'true'],
        results = {};

        // put your solution here

        return results;
    };
    var test = {
        "var"     : {},
        "this"    : {},
        "function": {},
        "if"      : {},
        "return"  : {},
        "true"    : {}
    },
    results = mySolution(test),
    pass = true;

    for (var prop in test)
        if (test.hasOwnProperty(prop))
            if (results[prop] !== test[prop])
                pass = false;

    alert(pass ? "PASS" : "FAIL")
}());

以下是我接受的两种可能的解决方案之一:

(function () {
    function mySolution ({
        var,
        this,
        function,
        if,
        return,
        true
    }) {
        // prohbit reference to arguments and the test object
        var test = arguments = null,

        args = ['var', 'this', 'function', 'if', 'return', 'true'],
        results = {};

        var i = args.length;
        while (i--) {
            results[args[i]] = eval("function::" + args[i]);
            // function::[args[i]] won't work unless you eval() it
        }

        return results;
    };
    var test = {
        "var"     : {},
        "this"    : {},
        "function": {},
        "if"      : {},
        "return"  : {},
        "true"    : {}
    },
    results = mySolution(test),
    pass = true;

    for (var prop in test)
        if (test.hasOwnProperty(prop))
            if (results[prop] !== test[prop])
                pass = false;

    alert(pass ? "PASS" : "FAIL")
}());

该解决方案的工作原理是将默认function::命名空间与eval()范围结合使用。

例如:foo.function::barfoo.function::['bar']是同一件事foo.bar

8 个答案:

答案 0 :(得分:4)

100分


(function () {
    function mySolution ({
        var,
        this,
        function,
        if,
        return,
        true
    }) {
        // prohbit reference to arguments and the test object
        var test = arguments = null,

        args = ['var', 'this', 'function', 'if', 'return', 'true'],
        results = {};

        // put your solution here
        var getEscUnicode = function(str) {
            var ret = "";
            for(var j = 0; j < str.length; j++) {
                var temp = parseInt(str.charCodeAt(j)).toString(16).toUpperCase();
                for(var i=0; i < 5 - temp.length; i++) {
                    temp = "0" + temp;
                }
                ret = ret + "\\u" + temp;
            }
            return ret;

        }
        for(var i = 0; i < args.length; i++) {
            results[args[i]] = eval(getEscUnicode(args[i]));
        }
        return results;
    };
    var test = {
        "var"     : {},
        "this"    : {},
        "function": {},
        "if"      : {},
        "return"  : {},
        "true"    : {}
    },
    results = mySolution(test),
    pass = true;

    for (var prop in test)
        if (test.hasOwnProperty(prop))
                if (results[prop] !== test[prop])
                        pass = false;

    alert(pass ? "PASS" : "FAIL")
}());

答案 1 :(得分:2)

在FireFox 3.0.13中测试过PASS! 我通过改变对象原型来“欺骗”:

<html>
<head>
<title></title>
<script>
(function () {
    function mySolution ({
        var,
        this,
        function,
        if,
        return,
        true
    }) {
        // prohbit reference to arguments and the test object
        var test = arguments = null,

        args = ['var', 'this', 'function', 'if', 'return', 'true'],
        results = {};

        // put your solution here
        Object.prototype._hasOwnProperty = Object.prototype.hasOwnProperty;
        Object.prototype.hasOwnProperty =
function(prop) {
 results[prop] = this[prop];
 return this._hasOwnProperty(prop);
}

        return results;
    };
    var test = {
        "var"     : {},
        "this"    : {},
        "function": {},
        "if"      : {},
        "return"  : {},
        "true"    : {}
    },
    results = mySolution(test),
    pass = true;

    for (var prop in test)
        if (test.hasOwnProperty(prop))
                if (results[prop] !== test[prop])
                        pass = false;

    alert(pass ? "PASS" : "FAIL")
}());

</script>
</head>
<body>
<!-- Put the body of your page below this line -->

<!-- Put the body of your page above this line -->
</body>
</html>

这算得上吗?我猜它可能没有。 = P

答案 2 :(得分:0)

作业?这是一个提示:使用eval

答案 3 :(得分:0)

我只能想到实现这一目标的一种方法,即使这种方式依赖于两者 - 已弃用的callee.callereval已经固定的FF特性能够在上下文中执行代码指定的功能。

有趣的是,我认为在引入eval扩展之前function({...}){}已“固定”,但我并不完全确定。

我稍微减少了测试用例,但当然保留了一个实际的想法。

我首先尝试从调用者本身访问arguments,但看起来<fn>.arguments在函数上下文中引用与arguments相同的对象; null参数对象基本上也是由arguments属性引用的对象。

我还考虑过从错误对象中获取堆栈字符串(以获取test值),但这不能解决任何问题,因为测试值是对象,而不是基元。

(function () {
  function mySolution () {
    var test = arguments = null;
    return eval('test', (function(){ return arguments.callee.caller; })());
  };
  var test = {
    "var"   : {},
    "this"  : {},
    "function": {},
    "if"    : {},
    "return"  : {},
    "true"  : {}
  },
  results = mySolution(test),
  pass = true;

  for (var prop in test)
    if (test.hasOwnProperty(prop))
        if (results[prop] !== test[prop])
            pass = false;

  alert(pass ? "PASS" : "FAIL");
})();

答案 4 :(得分:0)

啊哈!这次我找到了一个更好的答案。 (我不得不承认,我从kangax的答案得到了一般的想法)。在FF 3.0.13中测试通过:

<html>
<head>
<title></title>
<script>
(function () {
    function mySolution ({
        var,
        this,
        function,
        if,
        return,
        true
    }) {
        // prohbit reference to arguments and the test object
        var test = arguments = null,

        args = ['var', 'this', 'function', 'if', 'return', 'true'],
        results = {};

        // put your solution here
        var o = eval('arguments', mySolution)[0];
        for(var prop in o) {
         results[prop] = o[prop];
        }

        return results;
    };
    var test = {
        "var"     : {},
        "this"    : {},
        "function": {},
        "if"      : {},
        "return"  : {},
        "true"    : {}
    },
    results = mySolution(test),
    pass = true;

    for (var prop in test)
        if (test.hasOwnProperty(prop))
                if (results[prop] !== test[prop])
                        pass = false;

    alert(pass ? "PASS" : "FAIL")
}());

</script>
</head>
<body>
<!-- Put the body of your page below this line -->

<!-- Put the body of your page above this line -->
</body>
</html>

答案 5 :(得分:0)

尝试#3;再次,在FF 3.0.13中测试了PASS

<html>
<head>
<title></title>
<script>
(function () {
    function mySolution ({
        var,
        this,
        function,
        if,
        return,
        true
    }) {
        // prohbit reference to arguments and the test object
        var test = arguments = null,

        args = ['var', 'this', 'function', 'if', 'return', 'true'],
        results = {};

        // put your solution here
        var o = mySolution[0];
        for (var prop in o) {
           results[prop] = o[prop];
        }

        return results;
    };
    var test = {
        "var"     : {},
        "this"    : {},
        "function": {},
        "if"      : {},
        "return"  : {},
        "true"    : {}
    },
    results = mySolution(test),
    pass = true;

    for (var prop in test)
        if (test.hasOwnProperty(prop))
                if (results[prop] !== test[prop])
                        pass = false;

    alert(pass ? "PASS" : "FAIL")
}());

</script>
</head>
<body>
<!-- Put the body of your page below this line -->

<!-- Put the body of your page above this line -->
</body>
</html>

答案 6 :(得分:0)

尝试了很多方法。有点放弃。但如果您无法破坏系统,请更改系统。 我的解决方案:


(function () {
    function mySolution ({
        var,
        this,
        function,
        if,
        return,
        true
    }) {
        // prohbit reference to arguments and the test object
        var test = arguments = null,

        args = ['var', 'this', 'function', 'if', 'return', 'true'],
        results = {};

        // put your solution here
/********** MY SOLUTION STARTS ******************/
        return null;
    }
    function mySolution ({
        var,
        this,
        function,
        if,
        return,
        true
    }) {
        // new function does not prohbit reference to arguments and the test object
        //var test = arguments = null,

        args = ['var', 'this', 'function', 'if', 'return', 'true'],
        results = {};
        for(var i =0; i < args.length; i++) {
            results[args[i]] = arguments[0][args[i]];
        }
/********** MY SOLUTION ENDS ******************/
        return results;
    };
    var test = {
        "var"     : {},
        "this"    : {},
        "function": {},
        "if"      : {},
        "return"  : {},
        "true"    : {}
    },
    results = mySolution(test),
    pass = true;

    for (var prop in test)
        if (test.hasOwnProperty(prop))
                if (results[prop] !== test[prop])
                        pass = false;

    alert(pass ? "PASS" : "FAIL")
}());

答案 7 :(得分:0)

(function () {
    function mySolution ({ var, this, function, if, return, true }) {
    // prohbit reference to arguments and the test object
    var test = arguments = null, args = ['var', 'this', 'function', 'if', 'return','true'], results = {};
    //LAME...
    };
    mySolution=function(a){var results=a;
    //LAME...
    return results;
};
var test = {
      "var" : {},
      "this" : {},
      "function": {},
      "if" : {},
      "return" : {},
      "true" : {} }, results = mySolution(test), pass = true;
 for (var prop in test)
        if (test.hasOwnProperty(prop))
            if (results[prop] !== test[prop]) pass = false;
 alert(pass ? "PASS" : "FAIL") }());