[英]Why do array functions work like tagged template literals and what can I do with it?
Just recently I found this to be working:就在最近,我发现这是可行的:
const str = "foo bar";
const arr = str.split` `; // <- no parens
console.log(arr);
// -> ["foo", "bar"]
It seems like the split
prototype is a tagged template literal.似乎split
原型是一个标记的模板文字。 I also saw this working with other array functions like indexOf
.我还看到它与indexOf
等其他数组函数一起使用。
Is there a specific reason for implementing this behavior?实施此行为是否有特定原因? Can I do something with it that I could not do before or can I solve some tasks in a simpler way?我可以用它做一些我以前做不到的事情,或者我可以用更简单的方式解决一些任务吗?
It seems that there has been some effort put into the implementation, and every use-case that I can think of would just overcomplicate things.似乎已经为实现付出了一些努力,而我能想到的每个用例都会使事情变得过于复杂。 It seems to me like a waste of time to make it possible.在我看来,让它成为可能是浪费时间。 Or did it come at zero cost?还是零成本? If so: How?如果是这样:如何?
This is nothing special about the .split
method, this is just how tagged template literals work.这对于.split
方法没什么特别的,这就是标记模板文字的工作方式。 You can tag a template literal with any function, and it will be given as its first argument the parts of the template string outside of interpolation blocks.您可以使用任何function 标记模板文字,并且它将作为其第一个参数给出插值块之外的模板字符串部分。 For this case that would be the array [" "]
which then is used as the first argument for .split
(after which point [" "]
is converted to a string: " "
).在这种情况下,数组[" "]
将用作.split
的第一个参数(之后将[" "]
点转换为字符串: " "
)。
You can call any function by using it as a template tag.您可以将任何function 用作模板标签来调用它。
Template tag functions receive the following parameters:模板标签函数接收以下参数:
So, your call unwraps to:因此,您的电话展开为:
const arr = str.split([' ']);
String#split()
expects its first argument to be a string or regex. String#split()
期望它的第一个参数是字符串或正则表达式。 An array is neither of those, so .split()
coverts it to string by invoking its .toString()
method.数组不是这些,因此.split()
通过调用其.toString()
方法将其转换为字符串。
Array#toString()
returns the array's elements separated by commas. Array#toString()
返回以逗号分隔的数组元素。 In case of a single element, it returns the element converted into a string.如果是单个元素,则返回转换为字符串的元素。
As ' '
is already a string, it is returned, and used by .split()
to split the input string.由于' '
已经是一个字符串,它被返回,并被.split()
用来分割输入字符串。
Although there are some cases that can work like that, this usage (using functions as tag functions that wasn't designed for that) is discouraged, as it is unreadable, less performant (creates additional arrays, etc.) and saves only 2 characters ( ` `
instead of (' ')
), however, it is used by some minifiers and obfuscators.尽管在某些情况下可以这样工作,但不鼓励这种用法(将函数用作并非为此设计的标记函数),因为它不可读,性能较低(创建额外的 arrays 等)并且仅保存 2 个字符( ` `
而不是(' ')
),但是,它被一些压缩器和混淆器使用。
String.prototype.split
isn't actually designed to work with tagged template literals. String.prototype.split
实际上并不是为使用标记的模板文字而设计的。
When you call split
with a tagged template, the first parameter passed to the function is an array of strings in the template, so当您使用标记模板调用split
时,传递给 function 的第一个参数是模板中的字符串数组,所以
str.split` `;
is equivalent to相当于
str.split([" "]);
. . The array only has one string and there are no more parameters because there are no placeholders ( ${}
) inside the template literal.该数组只有一个字符串,并且没有更多参数,因为模板文字中没有占位符 ( ${}
)。
The spec says that the split
method first converts the first argument to a string and then works with it, so规范说split
方法首先将第一个参数转换为字符串,然后使用它,所以
str.split([" "]);
is equivalent to相当于
str.split(" ");
. .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.