javascript設計模式-繼承

javascript繼承分為兩種:類式繼承(原型鏈、extend函數)、原型式繼承(對繼承而來的成員的讀和寫的不對等性、clone函數)。

  1. 類式繼承-->prototype繼承:
     1     function Person(name){
     2         this.name = name;
     3     }
     4     Person.prototype.getName = function(){
     5         return this.name;
     6     }
     7     
     8     //繼承
     9     function Author(name,books){
    10         Person.call(this,name);
    11         this.books = books;
    12     }
    13     Author.prototype = new Person();
    14     Author.prototype.constructor = Author;
    15     Author.prototype.getBooks = function(){
    16         return this.books;
    17     }
    18     
    19     var author = new Author("zap","javascript程序設計");
    20     console.log(author);
  2. 類式繼承-->extend函數:
     1     function extend(subClass,superClass){
     2         var F = function(){};
     3         F.prototype = superClass.prototype;
     4         subClass.prototype = new F();
     5         subClass.prototype.constructor = subClass;
     6         
     7         subClass.superClass = superClass.prototype;
     8         if(superClass.prototype.constructor == Object.prototype.constructor){
     9             superClass.prototype.constructor =  superClass;
    10         }
    11     }
    12     
    13     
    14     /*Class Person*/
    15     
    16     function Person(name){
    17         this.name = name;
    18     }
    19     Person.prototype.getName = function(){
    20         return this.name;
    21     }
    22     
    23     function Author(name,books){
    24         Author.superClass.constructor.call(this,name);
    25 //        Person.call(this,name);
    26         this.books = books;
    27     }
    28     extend(Author,Person);
    29     Author.prototype.getBooks = function(){
    30         return this.books;
    31     }
    32     
    33     var author = new Author("zap","javascript程序設計");
    34     console.log(author);
    35     console.log(author.getName());
    36     console.log(author.getBooks());
  3. 原型式繼承-->clone函數:(原型式繼承更能節約內存)
     1     var Person  ={
     2         name:"zap",
     3         age:"26",
     4         toString:function(){
     5             console.log(this.name + "@" + this.age); 
     6         }
     7     }
     8     
     9     var author = clone(Person);
    10     author.name = "zap";
    11     author.toString();
    12     
    13     function clone(object){
    14         function F(){}
    15         F.prototype = object;
    16         return new F;
    17     }

    ?附上以類式繼承實現的就地編輯demo,原型式方式實現和類式繼承方式相差無幾,不在此列舉。

      1 <!DOCTYPE html>
      2 <html>
      3     <head>
      4         <meta charset="UTF-8">
      5         <title>類式繼承解決方案</title>
      6         <style type="text/css">
      7             #doc{width:500px; height:300px; border:1px solid #ccc; margin:10px auto;}
      8         </style>
      9     </head>
     10     <body>
     11         <div id="doc"></div>
     12     </body>
     13 </html>
     14 <script type="text/javascript">
     15     
     16     function EditInPlaceField(id,parent,value){
     17         this.id = id;
     18         this.value = value || "default value";
     19         this.parentElement = parent;
     20         
     21         this.createElements(this.id);
     22         this.attachEvents();
     23     }
     24     
     25     EditInPlaceField.prototype = {
     26         createElements:function(id){
     27             this.createContainer();
     28             this.createShowPanel();
     29             this.createEnterPanel();
     30             this.createControlBtns();
     31             this.convertToText();
     32         },
     33          //創建容器
     34         createContainer:function(){
     35             this.containerElement = document.createElement("div");
     36             this.parentElement.appendChild(this.containerElement);
     37         },
     38         createShowPanel:function(){
     39             this.staticElement = document.createElement("span");
     40             this.containerElement.appendChild(this.staticElement);
     41             this.staticElement.innerHTML  = this.value;
     42         },
     43         createEnterPanel:function(){
     44             this.fieldElement = document.createElement("input");
     45             this.fieldElement.type = "text";
     46             this.fieldElement.value = this.value;
     47             this.containerElement.appendChild(this.fieldElement);
     48         },
     49         createControlBtns:function(){
     50             this.saveButton = document.createElement("input");
     51             this.saveButton.type = "button";
     52             this.saveButton.value = "Save";
     53             this.containerElement.appendChild(this.saveButton);
     54             
     55             
     56             this.cancelButton = document.createElement("input");
     57             this.cancelButton.type = "button";
     58             this.cancelButton.value = "Cancel";
     59             this.containerElement.appendChild(this.cancelButton);
     60         },
     61         attachEvents:function(){
     62             var that = this;
     63             addEvent(this.staticElement,"click",function(){that.convertToEditable();});
     64             addEvent(this.saveButton,"click",function(){that.save();});
     65             addEvent(this.cancelButton,"click",function(){that.cancel();});
     66         },
     67         convertToEditable:function(){
     68             this.staticElement.style.display = "none";
     69             this.fieldElement.style.display = "inline";
     70             this.saveButton.style.display = "inline";
     71             this.cancelButton.style.display = "inline";
     72             
     73             this.setValue(this.value);
     74         },    
     75         save:function(){
     76             this.value = this.getValue();
     77             var that = this;
     78             var callback = {
     79                 success:function(){
     80                     that.convertToText();
     81                 },
     82                 failure:function(){
     83                     alert("Error saving value.");
     84                 }
     85             };
     86             
     87             ajaxRequest("get","save.php?id=",callback);
     88         },
     89         cancel:function(){
     90             this.convertToText();
     91         },
     92         convertToText:function(){
     93             this.fieldElement.style.display = "none";
     94             this.saveButton.style.display = "none";
     95             this.cancelButton.style.display = "none";
     96             this.staticElement.style.display = "inline";
     97             
     98             this.setValue(this.value);
     99         },
    100         setValue:function(value){
    101             this.fieldElement.value = value;
    102             this.staticElement.innerHTML = value;
    103         },
    104         getValue:function(){
    105             return this.fieldElement.value;
    106         }
    107     }
    108     
    109     //事件綁定
    110     function addEvent(element,type,fn){
    111         if(element.addEventListener){
    112             element.addEventListener(type,fn,false);
    113         }else if(element.attachEvent){
    114             element.attachEvent("on" + type,fn);
    115         }else{
    116             element["on" + type] = fn;
    117         }
    118     }
    119     //ajax請求
    120     function ajaxRequest(type,url,callback){
    121         callback.success();
    122     }
    123     //extend
    124     function extend(subClass,superClass){
    125         var F = function(){};
    126         F.prototype = superClass.prototype;
    127         subClass.prototype = new F();
    128         subClass.prototype.constructor = subClass;
    129         
    130         subClass.superClass = superClass.prototype;
    131         if(superClass.prototype.constructor == Object.prototype.constructor){
    132             superClass.prototype.constructor =  superClass;
    133         }
    134     }
    135     
    136     //子類
    137     function EditInPlaceArea(id,parent,value){
    138         EditInPlaceArea.superClass.constructor.call(this,id,parent,value);
    139     };
    140     extend(EditInPlaceArea,EditInPlaceField);
    141     
    142     //override
    143     EditInPlaceArea.prototype.createShowPanel = function() {
    144         this.staticElement = document.createElement("p");
    145         this.containerElement.appendChild(this.staticElement);
    146         this.staticElement.innerHTML = this.value;
    147     }
    148     
    149     EditInPlaceArea.prototype.createEnterPanel = function(){
    150         this.fieldElement =document.createElement("textarea");
    151         this.fieldElement.value = this.value;
    152         this.containerElement.appendChild(this.fieldElement);
    153     }
    154     
    155     EditInPlaceArea.prototype.convertToEditable = function(){
    156         this.staticElement.style.display = "none";
    157         this.fieldElement.style.display = "block";
    158         this.saveButton.style.display = "inline";
    159         this.cancelButton.style.display = "inline";
    160         
    161         this.setValue(this.value);
    162     }
    163     
    164     EditInPlaceArea.prototype.convertToText = function(){
    165         this.fieldElement.style.display = "none";
    166         this.saveButton.style.display = "none";
    167         this.cancelButton.style.display = "none";
    168         this.staticElement.style.display = "block";
    169         this.setValue(this.value);
    170     }
    171     
    172     var titleClassical = new EditInPlaceArea("titleClassical",document.getElementById("doc"),"title here");
    173     
    174 </script>
    View Code

    ?

轉載于:https://www.cnblogs.com/tengri/p/5271952.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458197.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458197.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458197.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

GIS基礎軟件及操作(二)

原文 GIS基礎軟件及操作(二) 練習二、管理地理空間數據庫 1.利用ArcCatalog 管理地理空間數據庫 2.在ArcMap中編輯屬性數據 第1步 啟動 ArcCatalog 打開一個地理數據庫 當 ArcCatalog打開后&#xff0c;點擊, 按鈕&#xff08;連接到文件夾&#xff09;. 建立到包含練習數據的…

libSVM分類小例C++

from&#xff1a;http://www.doczj.com/list_31/ 使用libSVM求解分類問題的C小例 1.libSVM簡介 訓練模型的結構體 struct svm_problem//儲存參加計算的所有樣本 { int l; //記錄樣本總數 double *y; //指向樣本類別的組數 //prob.y new double[prob.l]; struct svm_node …

qunit 前端腳本測試用例

首先引用qunit 測試框架文件 <link rel"stylesheet" href"qunit-1.22.0.css"> <script src"qunit-1.22.0.js"></script> <div id"qunit"></div> <div id"qunit-fixture"></div>…

非常規文件名刪除

生活中我們偶爾會遇到這樣一件事&#xff1a;走在路上&#xff0c;突然感覺鞋底有東西&#xff0c;抬腳一看&#xff0c;是個泡泡糖。拿不掉&#xff0c;走路還一粘一粘的。要多難受有多難受&#xff01;同樣在linux中也有這么一種文件名。看著不舒服&#xff0c;卻刪不掉。今天…

Machine Learning(Stanford)| 斯坦福大學機(吳恩達)器學習筆記【匯總】

from&#xff1a;https://blog.csdn.net/m399498400/article/details/52556168 定義本課程常用符號 訓練數據&#xff1a;機器用來學習的數據 測試數據&#xff1a;用來考察機器學習效果的數據&#xff0c;相當于考試。 m 訓練樣本的數量&#xff08;訓練集的個數) x 輸入的…

PHP OOP

類跟對象的關系類是對象的抽象(對象的描述(屬性)&#xff0c;對象的行為(方法))對象是類的實體面相對象的三大特征&#xff1a;封裝、集成、多態自定義類Class Person{}屬性定義屬性是類里面的成員&#xff0c;所以要定義屬性的前提條件是需要聲明一個類Class Person{public $n…

kv存儲對抗關系型數據庫

http://www.searchdatabase.com.cn/showcontent_52657.htm轉載于:https://www.cnblogs.com/hexie/p/5276034.html

模板匹配算法

from&#xff1a;https://blog.csdn.net/zhi_neng_zhi_fu/article/details/51029864 模板匹配(Template Matching)算法 模板匹配&#xff08;Template Matching&#xff09;是圖像識別中最具代表性的方法之一。它從待識別圖像中提取若干特征向量與模板對應的特征向量進行比較…

關于linux用戶權限的理解

創建用戶useradd 用戶名創建用戶組groupadd 組名查看用戶Idid 用戶修改文件權限chmod 777 文件名或目錄-R 遞歸修改用戶數組chown 屬主&#xff1a;屬組 文件名或目錄名-R 遞歸轉載于:https://blog.51cto.com/1979431/1833512

IMEI串號

IMEI串號就是國際移動設備身份碼&#xff0c;是電子設備的唯一身份證&#xff0c;由于它的唯一性&#xff0c;它可以用來查詢電子設備的保修期還有產地&#xff0c;可以說用處直逼人民的身份證啊&#xff01; 在撥號鍵盤頁面 輸入【*#06#】五個字符轉載于:https://www.cnblogs…

立體匹配十大概念綜述---立體匹配算法介紹

from&#xff1a;https://blog.csdn.net/wintergeng/article/details/51049596 一、概念 立體匹配算法主要是通過建立一個能量代價函數&#xff0c;通過此能量代價函數最小化來估計像素點視差值。立體匹配算法的實質就是一個最優化求解問題&#xff0c;通過建立合理的能量函數…

zjnu1730 PIRAMIDA(字符串,模擬)

Description Sample Input 6 JANJETINA 5 1 J 1 A 6 N 6 I 5 E Sample Output 1 0 2 1 1題意&#xff1a;給你一個長度小于等于10^6的字符串&#xff0c;然后每次讓它循環鋪蓋&#xff0c;構成層數為n的塔&#xff0c;讓你求得第i層塔中某個字符的個數。 思路&#xff1a;首先要…

ICP算法理解

from&#xff1a;https://blog.csdn.net/linear_luo/article/details/52576082 1 經典ICP ICP的目的很簡單&#xff0c;就是求解兩堆點云之間的變換關系。怎么做呢&#xff1f;思路很自然&#xff0c;既然不知道R和t(針對剛體運動)&#xff0c;那我們就假設為未知量唄&#xf…

2016-8-2更新日志

1.修正版本管理器資源文件名 不能正確拉取 91Resource 文件下的資源的問題2.修正商城購買物品不計算負重的問題3.修正拾取疊加物品 只計算一個物品的重量的問題4.游戲參數-> 游戲選項2->增加物品使用間隔5.修正冷酷不加技能點的BUG6.自定義UI開放測試[目前只能針對熱血傳…

字符流緩沖區的使用之BufferedWriter和BufferedReader

從字符輸入流中讀取文本&#xff0c;緩沖各個字符&#xff0c;從而實現字符、數組和行的高效讀取&#xff0c;代碼中使用了輸入緩沖區的特有的方法&#xff1a;readLine(),獲取一行文本數據 import java.io.BufferedReader; import java.io.FileNotFoundException; import java…

圖像處理的灰度化和二值化

from&#xff1a;http://blog.sina.com.cn/s/blog_13c6397540102wqtt.html 在圖像處理中&#xff0c;用RGB三個分量&#xff08;R&#xff1a;Red&#xff0c;G&#xff1a;Green&#xff0c;B&#xff1a;Blue&#xff09;&#xff0c;即紅、綠、藍三原色來表示真彩色&#x…

結合 category 工作原理分析 OC2.0 中的 runtime

絕大多數 iOS 開發者在學習 runtime 時都閱讀過 runtime.h 文件中的這段代碼: struct objc_class {Class isa OBJC_ISA_AVAILABILITY;#if !__OBJC2__Class super_class OBJC2_UNAVAILABLE;const char *name …

獲取子元素

1、純css 獲取子元素 #test1>div {background-color:red;}#test1 div {font-size:14px;}#test1>div:first-child {color:#ccc;} <div id"test1"><div>性別</div><div>男</div></div> 因1示例中為#test1下的子元素 #test1…

JPG PNG GIF BMP圖片格式的區別

類型優點缺點應用場景相同圖片大小比較BMP無損壓縮&#xff0c;圖質最好文件太大&#xff0c;不利于網絡傳輸 152KGIF動畫存儲格式最多256色&#xff0c;畫質差 53KPNG可保存透明背景的圖片畫質中等 202KJPG文件小&#xff0c;利于網絡傳輸畫質損失車牌識別84K BMP BMP&…

EasyUI左右布居

<!DOCTYPE html><html xmlns"http://www.w3.org/1999/xhtml"><head runat"server"> <meta http-equiv"Content-Type" content"text/html; charsetutf-8" /> <title>首頁</title> <li…