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

Errors in converting objects to basic types #168

Open
yjhtry opened this issue Dec 9, 2018 · 0 comments
Open

Errors in converting objects to basic types #168

yjhtry opened this issue Dec 9, 2018 · 0 comments

Comments

@yjhtry
Copy link

yjhtry commented Dec 9, 2018

The toString () method is invoked first when the date object is converted to a basic type. and Object calls toString () first when converted to a string

forexample one:
let date = new Date()
console.log(date.toString()); //Sun Dec 09 2018 16:48:43 GMT+0800 (中国标准时间)
console.log(date.valueOf()); //1544345323377

	console.log(date + '5');  //Sun Dec 09 2018 16:48:43 GMT+0800 (中国标准时间)4

forexample two:
let date = {name: 'yjh'}
console.log(date.toString()); //[object Object]
console.log(date.valueOf()); //{name: "yjh"}

	console.log(date + '5');  //[object Object]5

Your mistake:
对象在转换基本类型时,首先会调用 valueOf 然后调用 toString。

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