JAVASCRIPT基础学习篇(3)--ECMAScript Basic:constructor属性

The constructor property is a reference to the function that created an object.
constructor属性为所建立对象的函数参考。

constructor 属性是所有具有 prototype 的对象的成员。它们包括除 GlobalMath 对象以外的所有 JScript内部对象。constructor 属性保存了对构造特定对象实例的函数的引用

可以理解JAVA里面的构造方法,如下:

构造方法:

<script language="javascript">
function Student(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}

var zhangsan = new Student("zhangsan",18,"male");
alert(zhangsan.name)
alert(zhangsan.age)
alert(zhangsan.sex)
alert(zhangsan.constructor)

</script>

输出内容为:

zhangsan

18

male

function Student(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}

即alert(zhangsan.constructor)返回的内容为Student函数体。

可以利用constructor属性来判断对象的类型

如下:

var newDate = new Date();

if(newDate.constructor == Array){

alert('this is a array')

}else if(newDate.constructor == Date){

alert("this is a Date')

}else{

}

输出内容为:this is a Date

x = new String("Hi"); if (x.constructor == String) // 进行处理(条件为真)。 或 function MyFunc { // 函数体。 } y = new MyFunc; if (y.constructor == MyFunc) // 进行处理(条件为真)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值