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.mzph.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…