web开发专业javascript告诉我们object如何转换为number是不是错误?
Does it an error that professional javascript for web developers tell us how object convert to number?
看下面的代码,它应该输出什么?
let obj = Object.create(null);
obj.valueOf = function () { console.log('hit valueOf!'); return "TO BE NaN";};
obj.toString = function () { console.log('hit toString!'); return "1";};
Number(obj)
// ?
根据专业javascript, valueOf函数returns "TO BE NaN" , 将转换为NaN,然后调用toString函数。
- 转化次数
When applied to objects, the valueOf() method is called and the returned value is converted
based on the previously described rules. If that conversion results in NaN, the toString()
method is called and the rules for converting strings are applied.
事实上,它在这里显示了东西,未调用函数 toString !
let obj = Object.create(null);
obj.valueOf = function () { console.log('hit valueOf!'); return "TO BE NaN";};
obj.toString = function () { console.log('hit toString!'); return "1";};
Number(obj)
// hit valueOf!
// NaN
网络开发专业javascript告诉我们对象如何转换为数字是不是错误?
Number(value)
,根据规范,calls ToNumeric(value)
, which returns ToPrimitive(value, number)
. This second argument is the preferredType
, resulting in:
If hint is string, then
Let methodNames be « "toString", "valueOf" ».
Else,
Let methodNames be « "valueOf", "toString" ».
For each element name of methodNames, do
a. Let method be ? Get(O, name).
b. If IsCallable(method) is true, then
- Let result be ? Call(method, O).
- If Type(result) is not Object, return result.
所以在 Number(obj)
的情况下,确实应该先调用 valueOf
方法。
也就是说,下面这句话的出处是错误的:
When applied to objects, the valueOf() method is called and the returned value is converted based on the previously described rules. If that conversion results in NaN, the toString() method is called and the rules for converting strings are applied.
是的,这是你书中的一个错误(not the only one)。
应该更像1
When applied to objects, the valueOf()
method is called and the returned value is converted based on the previously described rules if it is a primitive value. If that conversion results in NaN it returns an object, the toString()
method is called and the rules for converting strings are applied.
展示:
const obj = Object.create(null);
obj.valueOf = function () { console.log('hit valueOf!'); return {};};
obj.toString = function () { console.log('hit toString!'); return "1";};
console.log(Number(obj))
1:虽然还是不太正确。应该提到的是,这两个方法只有在它们存在时才会被调用,如果它们都是 return 一个对象,则会抛出错误。
看下面的代码,它应该输出什么?
let obj = Object.create(null);
obj.valueOf = function () { console.log('hit valueOf!'); return "TO BE NaN";};
obj.toString = function () { console.log('hit toString!'); return "1";};
Number(obj)
// ?
根据专业javascript, valueOf函数returns "TO BE NaN" , 将转换为NaN,然后调用toString函数。
- 转化次数
When applied to objects, the valueOf() method is called and the returned value is converted based on the previously described rules. If that conversion results in NaN, the toString() method is called and the rules for converting strings are applied.
事实上,它在这里显示了东西,未调用函数 toString !
let obj = Object.create(null);
obj.valueOf = function () { console.log('hit valueOf!'); return "TO BE NaN";};
obj.toString = function () { console.log('hit toString!'); return "1";};
Number(obj)
// hit valueOf!
// NaN
网络开发专业javascript告诉我们对象如何转换为数字是不是错误?
Number(value)
,根据规范,calls ToNumeric(value)
, which returns ToPrimitive(value, number)
. This second argument is the preferredType
, resulting in:
If hint is string, then
Let methodNames be « "toString", "valueOf" ».
Else,
Let methodNames be « "valueOf", "toString" ».
For each element name of methodNames, do
a. Let method be ? Get(O, name).
b. If IsCallable(method) is true, then
- Let result be ? Call(method, O).
- If Type(result) is not Object, return result.
所以在 Number(obj)
的情况下,确实应该先调用 valueOf
方法。
也就是说,下面这句话的出处是错误的:
When applied to objects, the valueOf() method is called and the returned value is converted based on the previously described rules. If that conversion results in NaN, the toString() method is called and the rules for converting strings are applied.
是的,这是你书中的一个错误(not the only one)。
应该更像1
When applied to objects, the
valueOf()
method is called and the returned value is converted based on the previously described rules if it is a primitive value. Ifthat conversion results in NaNit returns an object, thetoString()
method is called and the rules for converting strings are applied.
展示:
const obj = Object.create(null);
obj.valueOf = function () { console.log('hit valueOf!'); return {};};
obj.toString = function () { console.log('hit toString!'); return "1";};
console.log(Number(obj))
1:虽然还是不太正确。应该提到的是,这两个方法只有在它们存在时才会被调用,如果它们都是 return 一个对象,则会抛出错误。