Symbol.split
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2020ë 1ì.
Symbol.split ì ì ë°ì´í° ìì±ì ì ìë ¤ì§ ì¬ë³¼ @@splitì ëíë
ëë¤. String.prototype.split() ë©ìëë íì¬ ê°ì²´ì ì¼ì¹íë ì¸ë±ì¤ìì 문ìì´ì ë¶í íë ë©ìëì 첫 ë²ì§¸ ì¸ììì ì´ ì¬ë³¼ì ì°¾ìµëë¤.
ë³´ë¤ ìì¸í ë´ì©ì RegExp.prototype[@@split]() ë° String.prototype.split()를 참조íì기 ë°ëëë¤.
ìëí´ ë³´ê¸°
class Split1 {
constructor(value) {
this.value = value;
}
[Symbol.split](string) {
const index = string.indexOf(this.value);
return `${this.value}${string.substring(0, index)}/${string.substring(
index + this.value.length,
)}`;
}
}
console.log("foobar".split(new Split1("foo")));
// Expected output: "foo/bar"
ê°
ì ìë ¤ì§ ì¬ë³¼ @@split.
Property attributes of Symbol.split | |
|---|---|
| ì°ê¸° ê°ë¥ | ë¶ê°ë¥ |
| ì´ê±° ê°ë¥ | ë¶ê°ë¥ |
| ì¤ì ê°ë¥ | ë¶ê°ë¥ |
ìì
>ì¬ì©ì ì ì ìë¶í
js
class ReverseSplit {
[Symbol.split](string) {
const array = string.split(" ");
return array.reverse();
}
}
console.log("Another one bites the dust".split(new ReverseSplit()));
// [ "dust", "the", "bites", "one", "Another" ]
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-symbol.split> |