本文實(shí)例講述了JavaScript實(shí)現(xiàn)多態(tài)和繼承的封裝操作。分享給大家供大家參考,具體如下:
黃陂網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)于2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
封裝Encapsulation
如下代碼,這就算是封裝了
(function (windows, undefined) { var i = 0;//相對(duì)外部環(huán)境來(lái)說(shuō),這里的i就算是封裝了 })(window, undefined);
繼承Inheritance
(function (windows, undefined) { //父類 function Person() { } Person.prototype.name = "name in Person"; //子類 function Student() { } Student.prototype = new Person(); //修復(fù)原型 Student.prototype.constructor = Student; //構(gòu)造函數(shù) Student.prototype.supr = Person.prototype; //父類 //創(chuàng)建子類實(shí)例 var stu = new Student(); Student.prototype.age = 28; Student.prototype.name = "name in Student instance"; //打印子類成員及父類成員 console.log(stu.name); //name in Student instance console.log(stu.supr.name); //name in Person console.log(stu.age); //28 })(window, undefined);
使用在線HTML/CSS/JavaScript代碼運(yùn)行工具 http://tools.jb51.net/code/HtmlJsRun,運(yùn)行結(jié)果如下:
多態(tài)Polymorphism
有了繼承,多態(tài)就好辦了
//這就是繼承了 (function (windows, undefined) { //父類 function Person() { } Person.prototype.name = "name in Person"; Person.prototype.learning = function () { console.log("learning in Person") } //子類 function Student() { } Student.prototype = new Person(); //修復(fù)原型 Student.prototype.constructor = Student; //構(gòu)造函數(shù) Student.prototype.supr = Person.prototype; //父類 Student.prototype.learning = function () { console.log("learning in Student"); } //工人 function Worker() { } Worker.prototype = new Person(); //修復(fù)原型 Worker.prototype.constructor = Worker; //構(gòu)造函數(shù) Worker.prototype.supr = Person.prototype; //父類 Worker.prototype.learning = function () { console.log("learning in Worker"); } //工廠 var personFactory = function (type) { switch (type) { case "Worker": return new Worker(); break; case "Student": return new Student(); break; } return new Person(); } //客戶端 var person = personFactory("Student"); person.learning(); //learning in Student person = personFactory("Worker"); person.learning(); //learning in Worker })(window, undefined);
使用在線HTML/CSS/JavaScript代碼運(yùn)行工具 http://tools.jb51.net/code/HtmlJsRun,運(yùn)行結(jié)果如下:
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
當(dāng)前題目:JavaScript實(shí)現(xiàn)多態(tài)和繼承的封裝操作示例
轉(zhuǎn)載來(lái)于:http://vcdvsql.cn/article4/pejjie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)、App設(shè)計(jì)、ChatGPT、網(wǎng)站維護(hù)、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)