为什么我不能直接将函数传递给replace方法?

时间:2021-03-29 21:03:21

I simply can't understand why the second and the third lines of output differs one from another:

我简直无法理解为什么第二行和第三行的输出彼此不同:

alphabet_ASCII = '';
for (i=65; i<=90; i++) {
  alphabet_ASCII += i;
}
alphabet_ASCII += '<br>';

document.body.innerHTML += alphabet_ASCII;



document.body.innerHTML += 
  alphabet_ASCII.replace(/\d{2}/g, x=>String.fromCharCode(x));

document.body.innerHTML += 
  alphabet_ASCII.replace(/\d{2}/g, String.fromCharCode);

What's the difference between x=>String.fromCharCode(x) and String.fromCharCode?

x => String.fromCharCode(x)和String.fromCharCode之间有什么区别?

1 个解决方案

#1


6  

Because String.fromCharCode accepts multiple arguments, and replace calls the callback with more than just the one argument you're expecting: It calls the callback with:

因为String.fromCharCode接受多个参数,并且替换调用回调不仅仅是你期望的一个参数:它调用回调:

  • All matching text
  • 所有匹配的文字
  • The contents of any capture groups (if any)
  • 任何捕获组的内容(如果有的话)
  • The index (offset) at which this match occurred
  • 发生此匹配的索引(偏移量)
  • The whole string being acted on
  • 整个字符串被执行

More on MDN.

有关MDN的更多信息。

So in your second example, String.fromCharCode gets more arguments than in your first, and does its best with them. On the first callback, String.fromCharCode gets "65", 0, "6566676869707172737475767778798081828384858687888990" and so returns "A\u0000\u0000" (because the second argument is 0 and the third is invalid). On the second pass it gets "66", 2, "6566676869707172737475767778798081828384858687888990" and returns "B\u0002\u0000", etc.

所以在你的第二个例子中,String.fromCharCode获取的参数比你的第一个更多,并且最好用它们。在第一个回调中,String.fromCharCode得到“65”,0,“6566676869707172737475767778798081828384858687888990”,因此返回“A \ u0000 \ u0000”(因为第二个参数为0,第三个参数无效)。在第二次传递它得到“66”,2,“6566676869707172737475767778798081828384858687888990”并返回“B \ u0002 \ u0000”等。

#1


6  

Because String.fromCharCode accepts multiple arguments, and replace calls the callback with more than just the one argument you're expecting: It calls the callback with:

因为String.fromCharCode接受多个参数,并且替换调用回调不仅仅是你期望的一个参数:它调用回调:

  • All matching text
  • 所有匹配的文字
  • The contents of any capture groups (if any)
  • 任何捕获组的内容(如果有的话)
  • The index (offset) at which this match occurred
  • 发生此匹配的索引(偏移量)
  • The whole string being acted on
  • 整个字符串被执行

More on MDN.

有关MDN的更多信息。

So in your second example, String.fromCharCode gets more arguments than in your first, and does its best with them. On the first callback, String.fromCharCode gets "65", 0, "6566676869707172737475767778798081828384858687888990" and so returns "A\u0000\u0000" (because the second argument is 0 and the third is invalid). On the second pass it gets "66", 2, "6566676869707172737475767778798081828384858687888990" and returns "B\u0002\u0000", etc.

所以在你的第二个例子中,String.fromCharCode获取的参数比你的第一个更多,并且最好用它们。在第一个回调中,String.fromCharCode得到“65”,0,“6566676869707172737475767778798081828384858687888990”,因此返回“A \ u0000 \ u0000”(因为第二个参数为0,第三个参数无效)。在第二次传递它得到“66”,2,“6566676869707172737475767778798081828384858687888990”并返回“B \ u0002 \ u0000”等。