您的位置:首页 > Web前端 > JavaScript

JavaScript面向对象编程(2)对象上的特殊属性

2014-12-05 21:07 288 查看
js对象都有constructor属性,但是构造函数产生的对象和简单对象的属性有些不同,可自行运行下列代码了解
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>对象上的特殊属性</title>
</head>

<body>
<script type="text/javascript">
function Hero(name) {
this.name = name;
this.occupation = 'Ninja';
this.whoAreYou = function() {
return "I'm " + this.name + " and I'm a " + this.occupation;
}
}
var h1 = new Hero('Michelangelo');
alert(h1.constructor);//返回一个函数
var obj = {};//var obj = new Object();
alert(obj.constructor);

</script>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息