Symbol的11个内置符号的使用场景

以下是11 个内置 Symbol 及其典型使用场景或面试题。


1. Symbol.iterator

用途 :定义对象的默认迭代器,用于 for...of、扩展运算符 ... 等。

ts 复制代码
const myIterable = {
  [Symbol.iterator]() {
    let step = 0;
    return {
      next() {
        step++;
        if (step <= 3) return { value: step, done: false };
        return { done: true };
      }
    };
  }
};

for (const v of myIterable) {
  console.log(v); // 1, 2, 3
}

面试题

如何让 { a: 1, b: 2, c: 3 }这样的普通对象支持 for...of循环?

ts 复制代码
const obj = {
  a: 1,
  b: 2,
  c: 3,
  *[Symbol.iterator]() {
    yield* Object.values(this)[Symbol.iterator]();
  },
};

for (const val of obj) {
  console.log(val); // 输出: 1, 2, 3
}

如何实现一个无限递增数字的迭代器?

ts 复制代码
const infiniteCounter = {
  *[Symbol.iterator]() {
    let count = 0;
    while (true) {
      yield count++;
    }
  },
};

const iterator = infiniteCounter[Symbol.iterator]();
console.log(iterator.next().value); // 0
console.log(iterator.next().value); // 1
console.log(iterator.next().value); // 2
// ...无限递增

2. Symbol.asyncIterator(ES2018)

用途 :异步数据流(如分页API)​,用于 for await...of

ts 复制代码
const asyncPager = {
  data: [1, 2, 3, 4, 5],
  pageSize: 2,
  currentPage: 0,
  async *[Symbol.asyncIterator]() {
    while (this.currentPage * this.pageSize < this.data.length) {
      const start = this.currentPage * this.pageSize;
      const end = start + this.pageSize;
      const chunk = this.data.slice(start, end);
      yield new Promise((resolve) => {
        setTimeout(() => resolve(chunk), 1000); // 模拟异步请求
      });
      this.currentPage++;
    }
  }
};

(async () => {
  for await (const page of asyncPager) {
    console.log(page); // [1, 2] (1秒后) → [3, 4] (1秒后) → [5] (1秒后)
  }
})();

3. Symbol.toStringTag

用途 :自定义 Object.prototype.toString.call(obj) 的输出。

ts 复制代码
class MyComponent {
  get [Symbol.toStringTag]() {
    return 'MyVueComponent';
  }
}

console.log(Object.prototype.toString.call(new MyComponent()));
// "[object MyVueComponent]"

应用场景:调试、日志、类型识别(如 Vue/React 组件标识)。


4. Symbol.hasInstance

用途 :自定义 instanceof 的行为。

ts 复制代码
class MyArray {
  static [Symbol.hasInstance](instance) {
    return Array.isArray(instance);
  }
}

console.log([1, 2] instanceof MyArray); // true

面试题

实现一个 NegativeNumber类,使得任何负数都是它的实例。

ts 复制代码
class NegativeNumber {
  static [Symbol.hasInstance](instance) {
    return typeof instance === 'number' && instance < 0;
  }
}

console.log(-5 instanceof NegativeNumber); // true
console.log(5 instanceof NegativeNumber); // false

5. Symbol.isConcatSpreadable

用途 :控制数组 concat 时是否展开。

ts 复制代码
const arr1 = [1, 2];
const arr2 = [3, 4];
arr2[Symbol.isConcatSpreadable] = false;

console.log(arr1.concat(arr2)); // [1, 2, [3, 4]]

6. Symbol.species

用途 :指定派生类构造函数(如 mapfilter 返回的实例类型)。

ts 复制代码
class MyArray extends Array {
  static get [Symbol.species]() {
    return Array; // map/filter 返回普通 Array 而非 MyArray
  }
}

const a = new MyArray(1, 2, 3);
console.log(a.map(x => x * 2) instanceof MyArray); // false

常见于:自定义集合类(如 Immutable.js、RxJS)。


7. Symbol.match

用途 :定义 String.prototype.match 调用时的行为。

ts 复制代码
const matcher = {
  [Symbol.match](str) {
    return str.includes('hello') ? ['hello'] : null;
  }
};

console.log('say hello'.match(matcher)); // ['hello']

8. Symbol.replace

用途 :定义 String.prototype.replace 的行为。

ts 复制代码
const replacer = {
  [Symbol.replace](str, substr) {
    return str.split(' ').join(substr);
  }
};

console.log('a b c'.replace(replacer, '-')); // "a-b-c"

9. Symbol.search

用途 :定义 String.prototype.search 的行为。

ts 复制代码
const searcher = {
  [Symbol.search](str) {
    return str.indexOf('world');
  }
};

console.log('hello world'.search(searcher)); // 6

10. Symbol.split

用途 :定义 String.prototype.split 的行为。

ts 复制代码
const splitter = {
  [Symbol.split](str) {
    return str.split(/[\s,]+/);
  }
};

console.log('a b,c'.split(splitter)); // ['a', 'b', 'c']

11. Symbol.unscopables

用途 :指定在 with 语句中被排除的属性(现代 JS 中极少使用)。

ts 复制代码
Array.prototype[Symbol.unscopables] = { copyWithin: true };

with ([]) {
  // copyWithin 不会被引入作用域
}

注意 :由于 with 已被严格模式禁用,此 Symbol 几乎无实际用途。

相关推荐
泯泷3 分钟前
Tiptap 深度教程(四):终极定制 - 从零创建你的专属扩展
前端·javascript·架构
局i4 分钟前
vue简介
前端·javascript·vue.js
yqcoder27 分钟前
vue2 和 vue3 中,列表中的 key 值作用
前端·javascript·vue.js
U***498329 分钟前
前端TypeScript教程汇总,从基础到高级
前端·javascript·typescript
梵得儿SHI29 分钟前
Vue 指令系统:事件处理与表单绑定全解析,从入门到精通
前端·javascript·vue.js·v-model·v-on·表单数据绑定·表单双向绑定
二川bro32 分钟前
第45节:分布式渲染:Web Workers多线程渲染优化
开发语言·javascript·ecmascript
秋天的一阵风1 小时前
😱一行代码引发的血案:展开运算符(...)竟让图表功能直接崩了!
前端·javascript·vue.js
Hilaku1 小时前
npm scripts的高级玩法:pre、post和--,你真的会用吗?
前端·javascript·vue.js
mCell2 小时前
React 如何处理高频的实时数据?
前端·javascript·react.js
随笔记2 小时前
HbuilderX载入项目,运行后唤起微信开发者工具,提示:Error: Fail to open IDE,唤醒不起来怎么办
javascript·微信小程序·uni-app