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

Number(1)和new Number(1)有什么区别 #8

Open
Chersquwn opened this issue Apr 21, 2020 · 0 comments
Open

Number(1)和new Number(1)有什么区别 #8

Chersquwn opened this issue Apr 21, 2020 · 0 comments

Comments

@Chersquwn
Copy link
Owner

Chersquwn commented Apr 21, 2020

区别

typeof 1 === 'number'    // true
typeof Number(1) === 'number'    // true
typeof new Number(1) === 'object'    // true

1 instanceof Number === false    // true
Number(1) instanceof Number === false    // true
new Number(1) instanceof Number === true    // true

1 instanceof Object === false    // true
Number(1) instanceof Object === false    // true
new Number(1) instanceof Object === true    // true

Object.prototype.toString.call(1) === '[object Number]'    // true
Object.prototype.toString.call(Number(1)) === '[object Number]'    // true
Object.prototype.toString.call(new Number(1)) === '[object Number]'    // true

为什么赋值了基本类型的变量能够调用方法?

读取变量时会创建基本包装类型,通过基本包装类型来调用对应的方法。

基本包装类型和引用类型的区别

生命周期的不同,基本包装类型在创建完调用方法后就会立即销毁。

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