arguments对象、apply()、匿名函数

在学习arguments对象时,碰到的一段code,不是太好理解。原文地址中文(http://www.jb51.net/article/25048.htm)、英文(http://www.sitepoint.com/arguments-a-javascript-oddity/)

想要正确理解,需要把握红色标注的地方。

1,function.prototype.apply()用法

2.需要明白majorTom在这里指向了匿名函数function(){return func.apply(null, args.concat(Array.prototype.slice.call(arguments)));}

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script>
        function format(string) {
            var args = arguments;
            var pattern = new RegExp("%([1-" + arguments.length + "])", "g");
            return String(string).replace(pattern, function(match, index) {
                return args[index];
            });
        }
        function makeFunc() {
            var args = Array.prototype.slice.call(arguments);
            var func = args.shift();
            return function () {
                return func.apply(null, args.concat(Array.prototype.slice.call(arguments)));
            };
        }

        var majorTom = makeFunc(format, "This is Major Tom to ground control. I'm %1.");
       console.log(majorTom("stepping through the door")) ;//majorTom是匿名函数的引用】

    </script>
</head>
<body>

</body>
</html>
原文地址:https://www.cnblogs.com/web-coding/p/4713014.html