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

来源:互联网 发布:网络作家富豪榜2017 编辑:程序博客网 时间:2024/05/16 00:16
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>

1 0
原创粉丝点击