简体   繁体   English

Javascript:哪些参数传递给函数?

[英]Javascript: What parameters are being passed to function?

I'm trying to wrap my brain around closures. 我试图将我的大脑包裹在封闭处。 I've been reading Javascript: the Good Parts, and the author presents this code snippet for adding a 'deentityify' method to String objects, the idea being to decode HTML-enocoded symbols: 我一直在阅读Javascript:The Good Parts,作者介绍了以下代码片段,用于向String对象添加“ deentityify”方法,该想法是对HTML编码符号进行解码:

// the method method:
Function.prototype.method = function(name, func) {
        if (! this.prototype[name]) {
                this.prototype[name] = func;
                return this;
        }
}

String.method('deentityify', function() {

        // entity table: maps entity names
        // to characters:
        var entity = {
                quot: '"',
                lt: '<',
                gt: '>',
        };

        // Return the deentityify method:
        return function() {
                return this.replace(/&([^&;]+);/g,
                        // What are a & b, how are they getting passed?!
                        function (a, b) {
                                //  console.log added by me, unsuccessfully:
                                console.log('a = ' + a);
                                console.log('b = ' + b);
                                var r = entity[b];
                                return typeof r === 'string' ? r : a;
                        }
                );
        };
}());

So I understand most of what's going on here, except for the actual parameters being passed to the function defined as the second parameter of String.replace. 因此,我了解这里发生的大多数事情,除了实际参数传递给定义为String.replace的第二个参数的函数。 How are 'a' and 'b' getting defined? 如何定义“ a”和“ b”?

Directly calling '"'.deentityify() does not explicitly pass any parameters. So how are they defined? As a secondary question, why does console.log() not work for logging the values of a and b? Is there any way I can successfully log such variables? 直接调用“'.deentityify()没有明确传递任何参数,因此它们是如何定义的?作为一个次要的问题,为什么CONSOLE.LOG()用于记录的值不能工作和b?有什么办法我能否成功记录此类变量?

Thanks for the help. 谢谢您的帮助。

EDIT: ' not ' was previously missing from the last sentence, making meaning unclear. 编辑:最后一句话以前没有' not ',因此含义不清楚。

The .replace() function can be called with a function as the second parameter. .replace()函数可以将函数作为第二个参数来调用。 When you do that, the function is invoked after the regex successfully matches with parameters extracted from the matching process. 这样做时,将在正则表达式成功匹配从匹配过程中提取的参数之后调用该函数。

The first parameter is always the entire matched portion of the source string. 第一个参数始终是源字符串的整个匹配部分。 Subsequent parameters represent the "capturing groups" from the regex. 后续参数表示来自正则表达式的“捕获组”。

In this case, the regular expression is /&([^&;]+);/g . 在这种情况下,正则表达式为/&([^&;]+);/g That matches an ampersand followed by any number of characters other than ampersand or semicolon, followed by a semicolon. 该字符与“&”号匹配,后跟“&”或分号以外的任意数量的字符,后跟一个分号。 The characters between the leading ampersand and the trailing semicolon are "captured" because that part of the regular expression is parenthesized. 前导“与”号和尾随的分号之间的字符被“捕获”,因为正则表达式的该部分带有括号。

Thus, if the source string is 因此,如果源字符串是

Hello &mdash; world!

then matching against that regular expression (once) would yield: 然后匹配该正则表达式(一次)将产生:

  • &mdash; as the overall matched string 作为整体匹配字符串
  • mdash as the value of the first (and only, in this case) captured group. mdash作为第一个(在这种情况下,也是唯一一个)捕获组的值。

Thus: 从而:

"Hello &mdash; world!".replace(/&([^&;]+);/g, function(all, group) {
  alert("entire match: " + all + " group: " + group);
});

would show a message according to what I listed above. 会根据我上面列出的内容显示一条消息。

Now, on top of all that, the code in the question involves another layer of interesting behavior. 现在,最重要的是,问题中的代码涉及另一层有趣的行为。 The function that is actually passed to the method() function is not that big outer function, but the function that is returned from that. 实际上传递给method()函数的函数不是那个大的外部函数,而是从该函数返回的函数。 That returned function expects to be called such that this is a String instance; 该返回的函数希望被调用,使得this是一个String实例; otherwise, the call to this.replace() won't work. 否则,对this.replace()的调用将无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM