//javascript 中的面向对象
//类:通过函数实现类的定义
function MyClass(name ,age){
this.name = name;//在函数内使用this给函数本身增加属性值
this.age = age;
this.prototype.toString = function(){ //使用函数的prototype对象来给函数添加方法
alert(this.name + " : " +this.age); //那么在创建一个新对象的时候,并不会复制
//这个函数的所有方法,而是指向了这个函数的所有方法
};
}
//如果要加入多个方法
MyClass.prototype = {
toString : function(){
alert(this.name + " : " +this.age);
}
hello : function(){
alert("hello world");
}
}
//静态类:函数本身就可以当作静态类来使用
var StaticClass = function(){};
StaticClass.name = "StaticClass";
StaticClass.Sum = function(var1,var2){
return var1 + var2;
};
alert(StaticClass.name);
alert(StaticClass.Sum(1,2));
//继承(inherit)
//one
function People(){
this.type = "人";
};
People.prototype = {
getType : function(){
alert("这是一个人");
}
};
function Student(name,sex){
People.apply(this,arguments);//使用apply方法将父对象的构造函数绑定在子对象上
this.name = name;
this.sex = sex;
var prop;//方法的继承,循环使用父对象的prototype进行复制
for(prop in People.prototype){
var proto = this.constructor.prototype;
if(!proto[prop]){
proto[prop] = People.prototype[prop];
}
proto[prop]["super"] = People.prototype;
}
}
//two
function People(){
this.type = "人";
this.getType = function(){
alert("这是一个人");
}
};
function Student(name,sex){
this.name = name;
this.sex = sex;
//通过对象冒充的方式进行继承
this.stu = People;
this.stu();
}
//three
function People3(name,sex){
this.name = name;
this.sex = sex;
this.type = "人";
this.getType = function(){
alert("这是一个人");
};
};
function Student3(){
People3.call(this,3,"nv");//通过父类调用自身来实现继承,第一个参数为子类对象
// this.name = name;
// this.sex = sex;
}
//类:通过函数实现类的定义
function MyClass(name ,age){
this.name = name;//在函数内使用this给函数本身增加属性值
this.age = age;
this.prototype.toString = function(){ //使用函数的prototype对象来给函数添加方法
alert(this.name + " : " +this.age); //那么在创建一个新对象的时候,并不会复制
//这个函数的所有方法,而是指向了这个函数的所有方法
};
}
//如果要加入多个方法
MyClass.prototype = {
toString : function(){
alert(this.name + " : " +this.age);
}
hello : function(){
alert("hello world");
}
}
//静态类:函数本身就可以当作静态类来使用
var StaticClass = function(){};
StaticClass.name = "StaticClass";
StaticClass.Sum = function(var1,var2){
return var1 + var2;
};
alert(StaticClass.name);
alert(StaticClass.Sum(1,2));
//继承(inherit)
//one
function People(){
this.type = "人";
};
People.prototype = {
getType : function(){
alert("这是一个人");
}
};
function Student(name,sex){
People.apply(this,arguments);//使用apply方法将父对象的构造函数绑定在子对象上
this.name = name;
this.sex = sex;
var prop;//方法的继承,循环使用父对象的prototype进行复制
for(prop in People.prototype){
var proto = this.constructor.prototype;
if(!proto[prop]){
proto[prop] = People.prototype[prop];
}
proto[prop]["super"] = People.prototype;
}
}
//two
function People(){
this.type = "人";
this.getType = function(){
alert("这是一个人");
}
};
function Student(name,sex){
this.name = name;
this.sex = sex;
//通过对象冒充的方式进行继承
this.stu = People;
this.stu();
}
//three
function People3(name,sex){
this.name = name;
this.sex = sex;
this.type = "人";
this.getType = function(){
alert("这是一个人");
};
};
function Student3(){
People3.call(this,3,"nv");//通过父类调用自身来实现继承,第一个参数为子类对象
// this.name = name;
// this.sex = sex;
}