Convert objec to a number with Number function(object's the valueOf() method is called) in JavaScript

Description

The following code shows how to convert objec to a number with Number function(object's the valueOf() method is called).

Example


<!-- w w w  .  ja va2s .  co  m-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var book = {
"title": "JavaScript",
"authors": ["J"],
edition: 3,
year: 2011,
valueOf: function(){
return 123;
}
};

var num = Number(book)
document.writeln(num);

</script>

</head>
<body>

</body>
</html>

Click to view the demo

The code above generates the following result.

Convert objec to a number with Number function(object