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>
时间: 2024-10-13 05:34:16