验证了下constructor属性。看js编译器怎么处理函数的constructor属性的
<!DOCTYPE HTML>
<html>
<head>
<title>constructor属性</title>
<meta http-equiv="content-type" content="text/html; charset=GB2312">
</head>
<body>
<script>
var Person = function(name,age){
this.name = name;
this.age = age;
}
console.log("Person.constructor === Function : "+(Person.constructor === Function));//true
console.log("Person.prototype.constructor === Person : "+(Person.prototype.constructor === Person));//true
//js编译器在函数定义的时候已经将prototype里的constructor指向函数
//new Person()返回的新对象都会继承这个constructor属性,他们的constructor都指向Person
var p = new Person(1,2);
console.log("p.constructor === Person : "+(p.constructor === Person));//true
p.constructor = "abc";
console.log("%o",p);//查看p对象属性
console.log("p.constructor === abc : "+(p.constructor === "abc"));//true constructor属性被覆盖
</script>
</body>
</html>
执行结果:
总结 1、函数在定义的时候js编译器已经将其prototype.constructor指向函数 2、constructor属性可被覆盖