Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null undefined 为什么没有包装类、symbol 为什么不能new? symbol 为什么不能有包装类? #8

Open
wangjbo opened this issue Jan 24, 2022 · 0 comments

Comments

@wangjbo
Copy link
Owner

wangjbo commented Jan 24, 2022

除了null和undefined,JS 里的原始类型都有对应的包装对象类型。

为什么要有包装对象?是为了能用.语法来读取属性、调用方法(对象才能有属性和方法),比如 "foo".length (1).toFixed(2)等代码中,都隐式的用到了包装对象。null和undefined不需要属性和方法,所以不需要包装对象。

symbol

  1. symbol作为ES6的一个新的开始,大胆做了一些变革,把 new Symbol()设计为抛出异常,而不是墨守成规的返回包装类对象
  2. new Symbol() 设计为抛出异常是因为在原型中的constructor方法中直接设计了抛出异常
  3. symbol 是有包装类的,Symbol就是包装类型,symbol('1').s = 1 不会报错,说明本身就是个包装类
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant