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>
  • 相关阅读:
    Java8之Consumer接口
    Java8之Cloneable接口
    Java基础之Iterator接口
    Java基础之Iterable接口
    WPF中窗体在同一个位置实现不同页面切换
    WPF中单选框RadioButton
    WPF中的TextBlock处理长字符串
    WPF中Canvas使用
    WPF中窗体调用窗体
    WPF中HyperLink超链接的使用
  • 原文地址:https://www.cnblogs.com/web-coding/p/4713014.html
Copyright © 2011-2022 走看看