js面向对象

1.第一种方法:

(function(){    //立即执行函数,保护私有变量
var n="SUN";
function Person(name){  //传参
var self={};    //定义对象
self.name=name;
self.sayHello=function(){
alert("PHello "+self.name+" "+n);
}
return self;   //返回对象
}
window.Person=Person;
}());
(function(){
function Teacher(name){
this.name=name;
var self=Person(name);
var surperSay=self.sayHello;
self.sayHello=function(){
surperSay.call(self);
alert("Thello "+self.name);
}
return self;
}
window.Teacher=Teacher;
}());
var t=Teacher('sun');
t.sayHello();

2.第二种方法:

var n="ime";
function People(name){
this.name=name;
}
People.prototype.say=function(){
alert("peo-hello "+this.name+" "+n)
}
window.People=People;
}());
(function(){
function Student(name){
this.name=name;
}
Student.prototype=new People();
var superSay=Student.prototype.say;
Student.prototype.say=function(){
superSay.call(this);
alert("stu-hello "+this.name);
}
window.Student=Student;
}());

var s=new Student("sun");
s.say();

posted @ 2017-03-09 14:51  sungang  阅读(105)  评论(0编辑  收藏  举报