JavaScript的join/split替代语法

huangapple go评论72阅读模式
英文:

Javascript join/split alternative syntax

问题

我已经使用JS有一段时间了,但它似乎永远不会停止让我感到惊讶(和/或恐惧)。

我刚刚偶然发现了String.split() / String.join()的另一种语法,而不是像正常人一样调用它,即:

const s = "abc-def-ghi"
const splitted = s.split("-")     // ["abc", "def", "ghi"]
const joined = splitted.join("/") // "abc/def/ghi"

你可以这样做:

const splitted = s.split`-`     // ["abc", "def", "ghi"]
const joined = splitted.join`/` // "abc/def/ghi"

所以我的问题是 - ?!?!??!?!!
我没有找到关于这个的任何文档,但至少在Node 8.1+中似乎可以工作。我不知道这是否是特定于Node的东西还是某种不常见的ES语法,但我想知道是否还有其他用法,以及它是否适用于其他方法。

英文:

I used JS for quite some time now, but it seems it will never cease to amaze (and/or horrify) me.

I just stumbled upon an alternative syntax for String.split() / String.join(), where instead of calling it like a normal human being, i.e.:

const s = "abc-def-ghi"
const splitted = s.split("-")     // ["abc", "def", "ghi"]
const joined = splitted.join("/") // "abc/def/ghi"

You do it like that:

const splitted = s.split`-`     // ["abc", "def", "ghi"]
const joined = splitted.join`/` // "abc/def/ghi"

So my question is - ?!?!??!?!!
I didn't find any documentation on this, but it seems to work in Node 8.1+ at least. I don't know if this is something node-specific or some obscure ES syntax, but I would like to know if there's more to it and if it works with other methods.

答案1

得分: 1

When ES6 was introduced, it also introduced tagged template literals. 这意味着字符串值通过前缀函数处理。

所以,在您的情况下,它是这样的:

const splitted = s.split`-`     // ["abc", "def", "ghi"]
const joined = splitted.join`/` // "abc/def/ghi"

split/join 作为一个函数,接受模板字符串字面量并进行处理。

英文:

When es6 was introduced it also introduced tagged template literals. Where a string value runs through a prefixed function.

So, in your case it is

const splitted = s.split`-`     // ["abc", "def", "ghi"]
const joined = splitted.join`/` // "abc/def/ghi"

split/join works as a function which takes the template string literals and processes it.


Take a sample demo:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function stronger(str){
    return `&lt;strong&gt;${str}&lt;/strong&gt;`;
}

let str = stronger`Let&#39;s get stronger.`;

document.body.innerHTML = str;

<!-- end snippet -->

答案2

得分: 1

这段代码是以一种巧妙的方式使用了“标记模板”(tagged templates)来将字符串参数应用于split/join函数。

标记模板函数的签名如下:

function tag(strings: String[], ...any)

当你调用Array.split()Array.join()时,它们接收一个包含一个字符串的数组:['-']['/']。这些函数期望一个字符串作为参数,因此会调用数组的toString()方法,将其转换为字符串,并将其用作split/join的参数。

示例:

function tag(strs) {
  console.log(strs.toString())
}

tag`-`
英文:

This code is using tagged templates in a hackish way to apply the string parameter to split/join.

The signature of a tagged template function is:

function tag(strings: String[], ...any)

When you call Array.split() or Array.join() they receive an array of one string: [&#39;-&#39;] or [&#39;/&#39;]. The functions expect a string, so the array's toString() method is called, which produces a string, and used a the parameter for the split/join.

Example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function tag(strs) {
  console.log(strs.toString())
}

tag`-`

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 20:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定