path:packages/shared/ReactSymbols.jsreact
// 用于标记类 React元素类型的符号。
// 若是没有原生的 Symbol 符号或 polyfill,则使用普通数字进行表示。
const hasSymbol = typeof Symbol === 'function' && Symbol.for;
export const REACT_ELEMENT_TYPE = hasSymbol
? Symbol.for('react.element')
: 0xeac7;
export const REACT_PORTAL_TYPE = hasSymbol
? Symbol.for('react.portal')
: 0xeaca;
export const REACT_FRAGMENT_TYPE = hasSymbol
? Symbol.for('react.fragment')
: 0xeacb;
export const REACT_STRICT_MODE_TYPE = hasSymbol
? Symbol.for('react.strict_mode')
: 0xeacc;
export const REACT_PROFILER_TYPE = hasSymbol
? Symbol.for('react.profiler')
: 0xead2;
export const REACT_PROVIDER_TYPE = hasSymbol
? Symbol.for('react.provider')
: 0xeacd;
export const REACT_CONTEXT_TYPE = hasSymbol
? Symbol.for('react.context')
: 0xeace;
export const REACT_ASYNC_MODE_TYPE = hasSymbol
? Symbol.for('react.async_mode')
: 0xeacf;
export const REACT_CONCURRENT_MODE_TYPE = hasSymbol
? Symbol.for('react.concurrent_mode')
: 0xeacf;
export const REACT_FORWARD_REF_TYPE = hasSymbol
? Symbol.for('react.forward_ref')
: 0xead0;
export const REACT_SUSPENSE_TYPE = hasSymbol
? Symbol.for('react.suspense')
: 0xead1;
export const REACT_MEMO_TYPE = hasSymbol ? Symbol.for('react.memo') : 0xead3;
export const REACT_LAZY_TYPE = hasSymbol ? Symbol.for('react.lazy') : 0xead4;
const MAYBE_ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
const FAUX_ITERATOR_SYMBOL = '@@iterator';
export function getIteratorFn(maybeIterable: ?any): ?() => ?Iterator<*> {
if (maybeIterable === null || typeof maybeIterable !== 'object') {
return null;
}
const maybeIterator =
(MAYBE_ITERATOR_SYMBOL && maybeIterable[MAYBE_ITERATOR_SYMBOL]) ||
maybeIterable[FAUX_ITERATOR_SYMBOL];
if (typeof maybeIterator === 'function') {
return maybeIterator;
}
return null;
}
复制代码
Symbol
是一个构造函数async
Symbol.for(key)
方法会根据给定的键 key
,来从运行时的 symbol 注册表中找到对应的 symbol,若是找到了,则返回它,不然,新建一个与该键关联的 symbol,并放入全局 symbol 注册表中。ide
获取可迭代对象的迭代器函数:函数
Symbol.iterator
属性:maybeIterable[Symbol.iterator]
@@iterator
属性:maybeIterable['@@iterator']
当 Symbol 不存在的时候,使用 0xeac7
这样的 16 进制数有何做用?为什么不像咱们其余的常量同样使用 一、2 等十进制数ui
Symbolspa