js面向对象

1.继承

1.1创建一个People类

function People(){

}

1.2.书写People类中的属性和方法

People.prototype.say=function() {

alert("哈哈");

}

1.3 创建一个Student类

function Student() {

}

1.4让Student继承自People

Student.prototype=new People();

//这代表student可以访问people自己的以及people原型链上的属性和方法。

2.重写

2.1 Student重写People的say方法

Student.prototype.say=function() {

alert("student hello");

};

3.重写后调用父类的方法

var stuSay=Student.prototype.say;//先拿指针指向父类的函数对象

Student.prototype.say=function() {

stuSay.call(this);//用this来调用上边保存下来的父类函数。

alert("student hello");

};

//注释:

1.先把Student原型上的方法保留下来。

2.然后调用时,用自己进行强制调用。就实现了消息的转发。

你可能感兴趣的:(js面向对象)