Home  >  Article  >  Web Front-end  >  Javascript constructor example analysis_javascript skills

Javascript constructor example analysis_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:58:101083browse

/*
*(REFER TO P151)
*@time 2008-11-25
*/

Copy code The code is as follows:

//No return value
function Test0(){
this.name='test0';
}
var test0=new Test0;
//debugger;
alert(test0);//output [Object]
alert(test0.name);//output test0
//return one character String object
function Test(){
this.name='test';
return new String('123');// Return string object
}
var test=new Test();
alert(test);//Output 123
alert(test.name);//Output undefined, indicating that the object created by the constructor is a string object
//return a primitive Type string
function Test2(){
this.name='test2';
return '123';// Return string object
}
var test2=new Test2() ;
alert(test2);//Output [Object]
alert(test2.name);//Output test0
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