[英]+ operator in Javascript
So I've noticed that the result of 所以我注意到了结果
(new Date())
is a Date object; 是一个Date对象; in this case
在这种情况下
Date {Thu Dec 04 2014 22:43:07 GMT+0200 (SAST)}
but if I type 但如果我输入
+(new Date())
I get an int value; 我得到一个int值;
1417725787989
How is this done? 这是怎么做到的?
I have a function called 'Duration' which, when used like this: 我有一个名为'Duration'的函数,当这样使用时:
new Duration(352510921)
returns an instance looking like this: 返回一个如下所示的实例:
{ days:5, hours:3, mins:55, secs:10, ms:921 }
So how can I use the + operator to get the int value of the Duration instance? 那么如何使用+运算符来获取Duration实例的int值?
var dur = new Duration(352510921);
console.log(+dur) // prints int value 352510921
The unary +
operator casts the instance to a Number
in the same way that calling the Number()
function will cast a variable to a Number
. 一元
+
运算符将实例转换为Number
,就像调用Number()
函数将变量转换为Number
。
If you'd like to override how your specific instance is cast, you need to override the valueOf
property for that instance: 如果您想覆盖特定实例的转换方式,则需要覆盖该实例的
valueOf
属性:
var a = {
valueOf: function () {
return 5;
}
};
console.log(a); //Object { valueOf: function () {...} }
console.log(+a); //5
From the ES5 standard : 从ES5标准 :
11.4.6 Unary + Operator # Ⓣ Ⓡ Ⓖ
11.4.6一元+算子#ⓉⓇⒼ
The unary + operator converts its operand to Number type.
一元+运算符将其操作数转换为数字类型。
The production UnaryExpression : + UnaryExpression is evaluated as follows:
生产UnaryExpression:+ UnaryExpression的计算方法如下:
ToNumber
on an Object will then result in a ToPrimitive
with hint Number. 然后,对象上的
ToNumber
将ToPrimitive
带有提示号的ToPrimitive
。 ToPrimitive
will then call the [[DefaultValue]]
internal method which states: 然后
ToPrimitive
将调用[[DefaultValue]]
内部方法,该方法指出:
When the [[DefaultValue]] internal method of O is called with hint Number, the following steps are taken:
当使用提示号调用O的[[DefaultValue]]内部方法时,将执行以下步骤:
- Let valueOf be the result of calling the [[Get]] internal method of object O with argument "valueOf".
设valueOf是使用参数“valueOf”调用对象O的[[Get]]内部方法的结果。
- If IsCallable(valueOf) is true then,
如果IsCallable(valueOf)为真,那么,
- Let val be the result of calling the [[Call]] internal method of valueOf, with O as the this value and an empty argument list.
设val是调用valueOf的[[Call]]内部方法的结果,其中O为此值和空参数列表。
- If val is a primitive value, return val.
如果val是原始值,则返回val。
- Let toString be the result of calling the [[Get]] internal method of object O with argument "toString".
设toString是使用参数“toString”调用对象O的[[Get]]内部方法的结果。
- If IsCallable(toString) is true then,
如果IsCallable(toString)为真,那么,
- Let str be the result of calling the [[Call]] internal method of toString, with O as the this value and an empty argument list.
令str为调用toString的[[Call]]内部方法的结果,其中O为此值,且为空参数列表。
- If str is a primitive value, return str.
如果str是原始值,则返回str。
- Throw a TypeError exception.
抛出TypeError异常。
You can override valueOf
. 您可以覆盖
valueOf
。
> var foo = { days:5, hours:3, mins:55, secs:10, ms:921, valueOf: function() { return 1; } }
undefined
> -foo
-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.