记事簿详情
返回

JavaScript the valueOf and toString method

Posted by: 梦飞无线

The valueOf is used to convert an object to the most suitable primitive value while the toString method is used to convert an object to the most suitable character string.

Most objects do not have a primitive equivalent, in this case the object itself is simply returned;Another problem you may encounter is the priority of these two method, by default, in a string operation circumstance the toString method take priority over the valueOf method, and in a number operation circumstance the valueOf method take priority over the toString method. Things are different when you define the valueOf method for a class, when you do this, even in a string operation circumstance the valueOf method take priority over the toString method, so in this case you may explicitly  call  the toString method when you need convert an object into string.


添加评论



点击换图 请输入验证码,不区分大小写