JavaScript中繼承用法實例分析
更新時間:2015年05月16日 09:11:59 作者:不吃皮蛋
這篇文章主要介紹了JavaScript中繼承用法,以實例形式較為詳細的分析了javascript實現(xiàn)繼承的相關技巧,需要的朋友可以參考下
本文實例分析了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程序設計有所幫助。
相關文章
微信小程序使用slider設置數(shù)據(jù)值及switch開關組件功能【附源碼下載】
這篇文章主要介紹了微信小程序使用slider設置數(shù)據(jù)值及switch開關組件功能,結合實例形式分析了slider組件及switch組件的功能與使用方法,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12
使用JavaScript將圖片合并為PDF的實現(xiàn)
在日常工作中,我們可能需要拍攝一些照片并將圖像合并到PDF文件中,這可以通過許多應用來完成,Dynamsoft Document Viewer讓這一操作更加方便,在本文中,我們將使用Dynamsoft Document Viewer創(chuàng)建一個Web應用,用JavaScript將圖像合并到PDF中,需要的朋友可以參考下2024-07-07
JavaScript中的"=、==、==="區(qū)別講解
今天小編就為大家分享一篇關于JavaScript中的"=、==、==="區(qū)別講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
JavaScript數(shù)組實現(xiàn)數(shù)據(jù)結構中的隊列與堆棧
這篇文章主要介紹了JavaScript數(shù)組實現(xiàn)數(shù)據(jù)結構中的隊列與堆棧的相關資料,需要的朋友可以參考下2016-05-05

