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 January 2020.

Thuộc tính dữ liệu tĩnh Symbol.split đại diện cho well-known symbol Symbol.split. Phương thức String.prototype.split() tra cứu symbol này trên đối số đầu tiên của nó cho phương thức chia chuỗi tại các chỉ mục khớp với đối tượng hiện tại.

Để biết thêm thông tin, xem RegExp.prototype[Symbol.split]()String.prototype.split().

Try it

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"

Giá trị

Well-known symbol Symbol.split.

Property attributes of Symbol.split
Writableno
Enumerableno
Configurableno

Ví dụ

Chia chuỗi đảo ngược tùy chỉnh

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" ]

Đặc tả

Thông số kỹ thuật
ECMAScript® 2027 Language Specification
# sec-symbol.split

Tương thích trình duyệt

Xem thêm