程序员人生 网站导航

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

栏目:htmlcss时间:2014-12-22 08:55:19
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⑻" /> <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);//返回1个函数 var obj = {};//var obj = new Object(); alert(obj.constructor); </script> </body> </html>

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐