@ruoli
2017-04-07T07:16:57.000000Z
字数 822
阅读 1521
Web前端
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>面向对象Js</title><script type="text/javascript">// 父类(function(){var n=123; //此属性 仅供 People类访问,外部无法访问function People(name){//构造方法,此处定义 People类this._name=name;}People.prototype.say = function(){ //为 People类 新增一个say方法alert("People-Hello "+this._name+" - "+n);}window.People=People; // People类 默认外部是不可调用的,此处暴露给window对象,使外部可以直接调用。}());// 子类(function(){function Student(name){ //定义类 Studentthis._name=name;}Student.prototype=new People();//继承父类 Peoplevar superSay=Student.prototype.say; //获取父类的 say方法Student.prototype.say=function(){ //子类重写父类的 say方法superSay.call(this);//子类中调用父类的方法alert("Student-Hello "+ this._name);}window.Student=Student; //将 Student 类暴露给 外部}());var student=new Student("ruoli"); // new 一个 Student 类的 对象student.say(); //调用 student 对象的 say 方法</script></head><body></body></html>