一般函数的写法:
function Person(name){
alert(name);
}
被称之构造函数的写法:
function Person(name){
this.name=name;
this.showMe=function(){
alert(this.name);
};
}
var p1=new Person("js")
p1.showMe(); // ‘js’;
使用function定义的对象与使用new操作符生成的对象之间有一个重要的区别。这个区别就是function定义的对象有一个prototype属性,使用new生成的对象就没有这个prototype属性。
prototype属性又指向了一个prototype对象,注意prototype属性与prototype对象是两个不同的东西,要注意区别。在prototype对象中又有一个constructor属性,这个constructor属性同样指向一个constructor对象,而这个constructor对象恰恰就是这个function函数本身。
有点头晕,看下图吧:
上面函数
alert(p1.prototype);//undefined 证明了new 的对象没有prototype属性
alert(typeof Person.prototype);//object
Person.prototype.constructor;//function Person(){} 就是Person构造函数
原文:http://www.cnblogs.com/webuserlast/p/4893418.html