当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Javascript 构造函数 实例分析

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 构造函数 实例分析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 173 ::
收藏到网摘: n/a

一般构造函数没有返回值,他们通过关键字this初始化对象,没有返回值。当然一个构造器允许返回一个对 象,如果这样的话 返回的对象将变成new 表达式的值,在这种情况下值为this的对象将抛弃 /*
*(REFER TO <JAVASCRIPT CORE>P151)
*@time 2008-11-25
*/
复制代码 代码如下:

//没有返回值
function Test0(){
this.name='test0';
}
var test0=new Test0;
//debugger;
alert(test0);//输出[Object]
alert(test0.name);//输出test0
//return 一个字符串对象
function Test(){
this.name='test';
return new String('123');// 返回字符串对象
}
var test=new Test();
alert(test);//输出123
alert(test.name);//输出undefined,说明有构造函数创建的对象是字符串对象
//return 一个原始类型字符串
function Test2(){
this.name='test2';
return '123';// 返回字符串对象
}
var test2=new Test2();
alert(test2);//输出[Object]
alert(test2.name);//输出test0