JavaScript中继承用法实例分析
作者:bea
本文实例分析了JavaScript中继承的用法。分享给大家供大家参考。具体如下: // define the Person Classfunction Person() {}Person.prototype.walk = function(){ alert ('I am walking!');};Person.prototype.sayHello = function(){ alert ('hello');};// define the Student classfunc
本文实例分析了JavaScript中继承的用法。分享给大家供大家参考。具体如下:
// define the Person Class
function Person() {}
Person.prototype.walk = function(){
alert ('I am walking!');
};
Person.prototype.sayHello = function(){
alert ('hello');
};
// define the Student class
function Student() {
// Call the parent constructor
Person.call(this);
}
// inherit Person
Student.prototype = new Person();
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
// replace the sayHello method
Student.prototype.sayHello = function(){
alert('hi, I am a student');
}
// add sayGoodBye method
Student.prototype.sayGoodBye = function(){
alert('goodBye');
}
var student = new Student();
student.sayHello();
student.walk();
student.sayGoodBye();
// check inheritance
alert(student instanceof Person); // true
alert(student instanceof Student); // true
希望本文所述对大家的javascript程序设计有所帮助。
有用 | 无用
// define the Person Class
function Person() {}
Person.prototype.walk = function(){
alert ('I am walking!');
};
Person.prototype.sayHello = function(){
alert ('hello');
};
// define the Student class
function Student() {
// Call the parent constructor
Person.call(this);
}
// inherit Person
Student.prototype = new Person();
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
// replace the sayHello method
Student.prototype.sayHello = function(){
alert('hi, I am a student');
}
// add sayGoodBye method
Student.prototype.sayGoodBye = function(){
alert('goodBye');
}
var student = new Student();
student.sayHello();
student.walk();
student.sayGoodBye();
// check inheritance
alert(student instanceof Person); // true
alert(student instanceof Student); // true
希望本文所述对大家的javascript程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- Jquery实现动态切换图片的方法
- JQuery中基础过滤选择器用法实例分析
- JQuery使用index方法获取Jquery对象数组下标的方法
- JQuery中属性过滤选择器用法实例分析
- jquery表单对象属性过滤选择器实例分析
- JQuery勾选指定name的复选框集合并显示的方法
- JavaScript的9种继承实现方式归纳
- JQuery中上下文选择器实现方法
- JQuery中两个ul标签的li互相移动实现方法
- JQuery球队选择实例
- JQuery实现动态添加删除评论的方法
- TypeError document.getElementById(...) is null错误原因
- JQuery实现带排序功能的权限选择实例
- JQuery中clone方法复制节点
- 分享十五款 jQuery 社交网络分享插件
- JS+CSS实现仿触屏手机拨号盘界面及功能模拟完整实例
- js实现精美的图片跟随鼠标效果实例
- js实现精美的银灰色竖排折叠菜单
- JavaScript实现鼠标滑过处生成气泡的方法