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>
  • 相关阅读:
    JQuery之动画效果
    JS (随着鼠标的移动,旁边显示放大图)
    jQuery 事件和动画
    jQuery 概述
    CSS (层叠样式表)
    css的文章部分的基本语句
    HTML基本语法
    Web
    JavaScript预解析案例,JavaScript预解析题目
    JavaScript预解析 变量提升与函数提升
  • 原文地址:https://www.cnblogs.com/web-coding/p/4713014.html
Copyright © 2011-2022 走看看