码迷,mamicode.com
首页 > 编程语言 > 详细

object to primitive in javascript

时间:2015-01-22 19:42:53      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

 

 例1:

var a={};  alert(a); //[object Object];

例2:

var a={

  toString:function(){

    return 1;

  }  

}

alert(a);  // 1

a+1;      //2

例3:

var a={toString:function(){return 1;},valueOf:function(){reuturn 2};}

alert(a); // 1

a+1;     // 3

---------------------------------------------------------------------------------------------------------------------------------------------------

+操作会如下步骤:

  1. Let lref be the result of evaluating AdditiveExpression.

  2. Let lval be GetValue(lref).

  3. Let rref be the result of evaluating MultiplicativeExpression.

  4. Let rval be GetValue(rref).

  5. Let lprim be ToPrimitive(lval).

  6. Let rprim be ToPrimitive(rval).

  7. If Type(lprim) is String or Type(rprim) is String, then

    1. Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)

  8. Return the result of applying the addition operation to ToNumber(lprim) andToNumber(rprim). See the Note below 11.6.3.

 

 

+ specification: http://es5.github.io/index.html#x11.6.1

 

object to primitive in javascript

标签:

原文地址:http://www.cnblogs.com/Mr-Joe/p/4242215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!