gpt4 book ai didi

javascript - 如果 `toString` 返回非原始值, “ignored” 是什么意思?

转载 作者:行者123 更新时间:2023-12-05 00:28:42 28 4
gpt4 key购买 nike

MDN documentation on Object.prototype.toString 说当toString被覆盖,它应该只返回一个原始值:

The toString() function you create must return a primitive, otherwise it will be ignored.


但是,在以下示例中,我们返回 toString 内部的一个对象。它正常返回对象:

const ob2 = {
val1: 100,
val2: 200,
toString: function() {
return {
veh: "meh"
}; // Object.
}
};

console.log(ob2.toString());

输出:
{ veh: "meh" }
所以 toString方法正常返回对象。错误信息是怎么回事?

最佳答案

你是对的,文档具有误导性且不完整。
我已提交 pull request将其改写如下:
删除了这部分:

The toString() function you create must return a primitive, otherwise it will be ignored.


取而代之:

The toString() function you create must return a primitive. If it returns an object and the method is called implicitly (i.e. during type conversion or coercion), then its result will be ignored and the value of a related method, valueOf(), will be used instead, or a TypeError will be thrown if none of these methods return a primitive.



我找到了原文 pull requestcommit添加了这个措辞。
作者在引用这句话时有一篇评论评论说:

See step 5.B.ii from https://262.ecma-international.org/9.0/#sec-ordinarytoprimitive


作者指的是 OrdinaryToPrimitive的后果规范中的抽​​象操作:离开 Symbol.toPrimitive 另外,当一个值被强制转换为一个原语时,这两种方法 toStringvalueOf (methodNames)准备好根据类型提示以特定顺序调用。
接着:
  1. For each element name of methodNames, do
    1. Let method be ? Get(O, name).
    2. If IsCallable(method) is true, then
      1. Let result be ? Call(method, O).
      2. If Type(result) is not Object, return result.

这一步是一个循环,遍历方法名称列表。
它从这个列表中获取下一个方法,检查它是否是一个函数,调用它,并将其结果存储在结果中。
然后它执行类型检查。
如果结果是原始的,即不是对象,则返回此结果。
否则, 循环继续,实际上忽略了结果 .
如果循环到达末尾没有返回值,则会抛出 TypeError。
为了演示此行为,您必须同时拥有这两种方法:

class Test1 {
toString() {
return "1";
}
valueOf() {
return "2";
}
}

class Test2 {
toString() {
return {};
}
valueOf() {
return "2";
}
}

class Test3 {
toString() {
return "1";
}
valueOf() {
return {};
}
}

class Test4 {
toString() {
return {};
}
valueOf() {
return {};
}
}

const test1 = new Test1,
test2 = new Test2,
test3 = new Test3,
test4 = new Test4;

console.log(String(test1)); // "1"; toString is preferred.
// `"" + test1` and `+test1` also demonstrate this.
console.log(Number(test1)); // 2; valueOf is preferred.
console.log(String(test2)); // "2"; toString is ignored; valueOf is chosen instead.
console.log(Number(test2)); // 2; valueOf is preferred.
console.log(String(test3)); // "1"; toString is preferred.
console.log(Number(test3)); // 1; valueOf is ignored; toString is chosen instead.
console.log(String(test4)); // TypeError; none of the methods returns a primitive.
console.log(Number(test4)); // TypeError; none of the methods returns a primitive.


文档没有提到的是 明确 调用 toString方法,因为它工作得很好:

class Test {
toString() {
return {
hello: "world"
};
}
}

const test = new Test;

console.log(test.toString()); // Logs { hello: "world" }.

关于javascript - 如果 `toString` 返回非原始值, “ignored” 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71194027/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com