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

关于 Object.prototype.toString 的讨论 #196

Open
jxccc1998 opened this issue Jan 29, 2021 · 4 comments
Open

关于 Object.prototype.toString 的讨论 #196

jxccc1998 opened this issue Jan 29, 2021 · 4 comments

Comments

@jxccc1998
Copy link

@Tan90Qian 厉害了,我也才发现,原来:

Object.prototype.toString.call(location)
// "[object Location]"
Object.prototype.toString.call(history)
// "[object History]"

而且他们跟自身相等是因为引用是相同的吧,就像:

var obj = {};
console.log(obj === obj) // true

Originally posted by @mqyqingfeng in #28 (comment)

@jxccc1998
Copy link
Author

请问既然Object.prototype.toString.call(window)会得到window,为什么全局直接执行Object.prototype.toString()不返回window

@xixizhangfe
Copy link

请问既然Object.prototype.toString.call(window)会得到window,为什么全局直接执行Object.prototype.toString()不返回window

Object.prototype.toString.call(window)实际会调用window[Symbol.toStringTag]。
window[Symbol.toStringTag] // Window

@jxccc1998
Copy link
Author

那如果window.Object.prototype.toString(),执行方法中的this是指向Object,为啥不是window

@huangpingcode
Copy link

请问既然Object.prototype.toString.call(window)会得到window,为什么全局直接执行Object.prototype.toString()不返回window

因为不是window调用toString,注意this指向,a.b.c.funfun的this是c,不是a

@mqyqingfeng mqyqingfeng changed the title @Tan90Qian 厉害了,我也才发现,原来: 关于 Object.prototype.toString 的讨论 Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants