1. javascript中的类即函数
/*==============================类即函数=====================================*/
function People(name,sex,addr){
this.name = name;
this.sex = sex;
this.addr = addr;
this.foo = function(){
alert(this.name + this.sex+this.addr);
}
}
var people = new People('张三','男','浙江杭州');
people.foo(); //张三男浙江杭州
var newp = new People('历史','男','浙江杭州');
/*这里不能使用对象代替函数名,且添加的函数属性是共有的*/
People.prototype.getName = function(){
alert(this.name);
}
/*这里添加的函数属性 仅仅是当前对象私有的*/
people.getName1 = function(){
alert(this.name);
}
/**/
people.getName(); //张三
people.getName1(); //张三
newp.getName(); //历史2. javascript中的特殊对象
/*=============================自定义的对象 可以理解为单例模式================================*/
var Human = {
name:'',
sex:'',
addr:'',
createh:function(name,sex,addr){
this.name = name;
this.sex = sex;
this.addr = addr;
},
foo : function(){
alert(this.name + this.sex+this.addr);
}
};
Human.createh('李梅','女','浙江杭州')
Human.foo(); //李梅女浙江杭州
原文:http://blog.csdn.net/wujiangwei567/article/details/45154697