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

valueOf与 toString #16

Open
gdutwyg opened this issue Jul 15, 2018 · 1 comment
Open

valueOf与 toString #16

gdutwyg opened this issue Jul 15, 2018 · 1 comment
Labels

Comments

@gdutwyg
Copy link
Owner

gdutwyg commented Jul 15, 2018

  • valueOf():返回最适合该对象类型的原始值;
  • toString(): 将该对象的原始值以字符串形式返回。这两个方法一般是交由JS去隐式调用,以满足不同的运算情况。
Number({})  // NaN   等同于Number("[object Object]")

因为你传参是一个对象,它属于复合数据类型,会隐式调用valueOf方法。
1.隐式转换过程中,valueOf如果能返回基本数据类型则不会往下计算;
2.valueOf如果不能返回基本数据类型则会继续调用toString方法,toString方法如果还不能自动转换为基本数据类型则报错!

从++[[]][+[]]+[+[]]==10?深入浅出弱类型JS的隐式转换

@gdutwyg gdutwyg added the js label Jul 15, 2018
@gdutwyg
Copy link
Owner Author

gdutwyg commented Aug 30, 2018

(1314).toString(16) // ‘522’
parseInt('522', 16) // 1314

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant