Skip to content

对象的Symbol.toStringTag属性,指向一个方法。这句“指向一个方法”是否是错误呢? #1166

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

Closed
yinsang opened this issue Aug 22, 2023 · 1 comment

Comments

@yinsang
Copy link

yinsang commented Aug 22, 2023

const test = document.createElement("button");
test.toString(); // "[object HTMLButtonElement]"
test[Symbol.toStringTag]; // "HTMLButtonElement"

根据实际例子应该是属性是字符串而不是指向一个方法。
实际上

class Collection {
  get [Symbol.toStringTag]() {
    return 'xxx';
  }
}
let x = new Collection();
Object.prototype.toString.call(x) // "[object xxx]"

中Collection的[Symbol.toStringTag] 在实例化的时候变成了字符串,也不应该说是指向一个方法。

// 字符串'xxx'
x[Symbol.toStringTag]

而我试了放方法在里面是无法修改第二个Object的。

var obj = {[Symbol.toStringTag](){return 'xxx'}};
obj.toString() 
// '[object Object]'
@ruanyf
Copy link
Owner

ruanyf commented Aug 22, 2023

对象的Symbol.toStringTag属性,指向一个方法。

是这一句吗?

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

2 participants