Home  >  Article  >  Web Front-end  >  JavaScript plus sign () operator symbol_javascript skills

JavaScript plus sign () operator symbol_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:40:161121browse

1. The operator operation process for reference type objects (I refer to String, Date, Object, Array, Function, Boolean) is as follows!
1. First call the valueOf method of this object to get the return value A
2. Then convert this value A into a number and get the final value

My test is as follows:

Copy code The code is as follows:

function w(s){
document.writeln("
");
document.writeln(s);
document.writeln("
--------------------------------");
}
String.prototype.valueOf=function(){return 1;};
w( new String("sss"));//Output 1
String.prototype.valueOf=function(){return "a ";};
w( new String("sss"));//Output NaN


Date.prototype.valueOf=function(){return 1;};
w ( new Date());//Output 1
Date.prototype.valueOf=function(){return "a";};
w( new Date());//Output NaN

Object.prototype.valueOf=function(){return 1;};
w( {});//Output 1
Object.prototype.valueOf=function(){return "a";};
w( {});//Output NaN

Array.prototype.valueOf=function(){return 1;};
w( []);//Output 1
Array.prototype.valueOf=function(){return "a";};
w( []);//Output NaN

var s=function(){};
Function. prototype.valueOf=function(){return 1;};
w( s);//Output 1
Function.prototype.valueOf=function(){return "a";};
w( s);//Output NaN

Boolean.prototype.valueOf=function(){return 1;};
w( new Boolean());//Output 1
Boolean.prototype. valueOf=function(){return "a";};
w( new Boolean());//Output NaN

Two, for basic data data types, the value is converted into a number
Copy code The code is as follows:

w( 5);//output 5
w( true);//Output 1
w( false); // Output 0
w( "ss"); // Output NaN
w( "111"); // Output 111
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn