zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    [js]vue-router的使用
    [js]递归实现 数组转树形
    [js]vue组件核心
    [js]了解chart绘图
    [js]vue权限控制
    [js]vue显示一个外部链接的组件
    [js]axios使用
    [js]vue中 给router-view 组件的 绑定 key 的原因
    [java]BeanPostProcessor使用及源码
    [java]权限管理
  • 原文地址:https://www.cnblogs.com/web-coding/p/4713014.html
Copyright © 2011-2022 走看看