javascript --- 类、class、事件委托的编程风格

类风格:

// 父类
function Widget(width, height) {this.width = width || 50;this.height = height || 50;this.$elem = null;
}
Widget.prototype.render = function($where) {if(this.$elem) {this.$elem.css({width: this.width + "px",height: this.height + "px"}).appendTo($where);}
};// 子类
function Button(width, height, label) {// 调用父类(Widget)的构造函数Widget.call(this, width, height);this.label = label || "Default";this.$elem = $("<button>").text(this.label);
}// 让Button"继承"Widget(Button可以使用Widget原型上的方法,本例中是render方法)
Button.prototype = Object.create(Widget.prototype);// 重写render
Button.prototype.render = function($where) {// 调用父类(Widget)的render方法Widget.prototype.render.call(this, $where);  this.$elem.click(this.onClick.bind(this)); // 监听点击事件
};Button.prototype.onClick = function(evt) {console.log("Button '" + this.label + " 'clicked!" );
};$(document).ready(function() {var $body = $(document.body);var btn1 = new Button(125, 30, "Hello" );var btn2 = new Button(150, 40, "World");btn1.render($body);btn2.render($body);
});// 注:用到了jquery故需要导入cdn <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>

在这里插入图片描述
在这里插入图片描述
上述代码,首先定义了一个Widget的父类,然后使用使用在constructor中使用call方法(作用域绑定到当前)调用父类Widget的初始化…并添加了一个新的$elem(即Butoon标签).
在Button的原型上,使用Object.create()方法继承父类的原型方法(render).并新增了一个(按钮特有的)点击功能.

说明:上面是一个仿照类实现的方法,其实就是使用Widget.call 和Widget.prototype.render.call在Button函数中调用Widget…
用书上的原话说就是…丑陋的显示多态

ES6的class语法糖实现:

class Widget{constructor(width, height) {this.width = width || 50;this.heigth = height || 50;this.$elem = null;}// 很巧妙的render...render($where) {if(this.$elem) {this.$elem.css({width: this.width + "px",height: this.height + "px"}).appendTo($where);}}
}
class Button extends Widget{constructor(width, height, label) {super( width, height);this.label = label || "Default";this.$elem = $("<button>").text(this.label);  // 生成一个<button> 内容是label}render($where) {  // $where 是调用时传入的参数super.render($where);   // 调用父类Widget的renderthis.$elem.click(this.onClick.bind(this));}onClick(evt) {console.log("Button ' " + this.label + " ' clicked!");}
}
$(document).ready(function(){var $body = $(document.body);var btn1 = new Button(125, 30, "Hello" );var btn2 = new Button(150, 40, "World" );btn1.render($body);btn2.render($body);})
//说明: 关键是super替代了伪类中的call方法...变得更简洁(at least看上去是这样...)

委托控件对象:

var Widget = {init: function(width, height){this.width =width || 50;this.height = height || 50;this.$elem = null;},insert: function($where) {if(this.$elem){this.$elem.css({width: this.width + "px",height: this.heigth + "px"}).appenTo($where);}}
};
// 还是定义一个Widget对象,, 使用委托(Object.create)顺着原型链[[prototype]]由下而上读取方法
var Button = Object.create(Widget);Button.setup = function(width, height, label) {this.init(width, height);  // Button无init方法,顺着原型链找到了Widget中的init.this.label = label || "Default";this.$elem = $('<button>').text(this.label);
};
Button.build = function($where) {this.insert($where);this.$elem.click(this.onClick.bind(this));  // this.onClick实际上是Button.onClick
};
Button.onClick = function(evt) {console.log("Button '" + this.label + "' clicked!" );
};
$(document).ready(function(){var $body = $(document.body);var btn1 = Object.create(Button);btn1.setup(123, 30, 'Hello');var btn2 = Object.create(Button);btn2.setup(150, 40, 'World');btn1.build($body);btn2.build($body);
});
// 很美的方式

参考《你不知道的JavaScript》(上卷) P174 ~P177

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/251229.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在线获取UUID

http://fir.im/udid转载于:https://www.cnblogs.com/mtjbz/p/8116576.html

堆和堆排序

堆和优先队列 普通队列&#xff1a;FIFO&#xff0c;LILO 优先队列&#xff1a;出队顺序和入队顺序无关&#xff0c;和优先级相关。一个典型应用就是操作系统中。动态选择优先级高的任务执行 堆的实现 最典型的堆就是二叉堆&#xff0c;就像是一颗二叉树。这个堆的特点&#xf…

ES5-1 发展史、ECMA、编程语言、变量、JS值

1. 5大主流浏览器及内核&#xff08;自主研发&#xff09; 浏览器内核IEtridentChromewebkit blinkSafariwebkitFirefoxgeckoOperapresto 2. 浏览器的历史 和 JS诞生 1989-1991 WorldWideWeb&#xff08;后来为了避免与万维网混淆而改名为Nexus&#xff09;是世界上第一个网页…

javascript --- 使用对象关联简化整体设计

在某个场景中,我们有两个控制器对象: 1.用来操作网页中的登录表单; 2.用来与服务器进行通信. 类设计模式 // 把基础的函数定义在名为Controller的类中,然后派生两个子类LoginController和AuthController. // 父类 function Controller() {this.errors []; } Controller.prot…

javascript --- polyfill中几个常用方法

ES6中,新增了许多有用的方法,下面分享几个ES6之前得版本写的polyfill Number.EPSILON: // 机器精度,并判断2个数是否相等 if(!Number.EPSILON){Number.EPSILON math.pow(2, -52); }function numberCloseEnoughToEqual(n1, n2) {return Math.abs(n1 - n2 ) < Number.EPSIL…

[Usaco2010 Nov]Visiting Cows

题目描述 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<N<50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统,里面有N-1条路,每条路连接了一对编号为C1和C2的奶牛(1 < C1 < N; 1 < C2 < N; C1…

ES5-2 语法、规范、错误、运算符、判断分支、注释

1. 错误 MDN错误列表 Uncaught SyntaxError: Unexpected token ) // 语法错误 Uncaught ReferenceError: a is not defined // 引用错误等类型 Uncaught TypeError: Cannot read property toString of null出现一个语法错误&#xff0c;则一行代码都不会执行&#xff08;检查…

新单词 part 4

part 41.veto 英[ˈvi:təʊ]美[ˈvi:toʊ]n. 行使否决权; 否决权&#xff0c;否认权; 否决理由;vt. 否决&#xff0c;不同意; 不批准&#xff0c;禁止;vi. 否决; 禁止;2.acoustics 英[əˈku:stɪks]美[əˈkustɪks]n. 声学; &#xff08;传声系统的&#xff09; 音响效果; 声…

unity深度查找某个子物体和遍历所有子物体方法

本文总结一下关于unity的查找子物体的方法 首先说明一下这里将讲三种查找子物体方法&#xff1a; 查找固定路径的某一个子物体的方法、通过名字深度查找某个子物体的方法、查找父物体下所有子物体的方法。 第一:查找固定路径的某一个子物体的方法 对于已知的路径可以直接用go.t…

javascript --- JSON字符串化

工具函数JSON.stringify()将JSON对象序列化为字符串时也用到了ToString. 看下面的代码: console.log(JSON.stringify(42)); console.log(JSON.stringify("42")); console.log(JSON.stringify(null)); console.log(JSON.stringify(true));所有安全的JSON值都可以使用…

静态链接和动态链接

静态链接和动态链接 静态链接方法&#xff1a;静态链接的时候&#xff0c;载入代码就会把程序会用到的动态代码或动态代码的地址确定下来 静态库的链接可以使用静态链接&#xff0c;动态链接库也可以使用这种方法链接导入库 动态链接方法&#xff1a;使用这种方式的程序并不在一…

ES5-3 循环、引用值初始、显示及隐式类型转换

1. 循环 for循环的三个参数abc&#xff0c;a只执行一次&#xff0c;c在每次循环后执行 // 打印0-100的质数 1不是质数 var list [2] for (var i 3; i < 100; i i 2) {var flag falsefor (var j 0; j < list.length; j) {var cur list[j]if (i % cur 0 &…

hihocoder 二分

题目 一个简单的二分&#xff0c;只是想说明一下&#xff0c;如若要查找一个数组中某个数的下标可以直接用lower_bound()这个函数。只是要考虑到要查找的数不在数组中的这种情况。 #include <cstdio> #include <iostream> #include <algorithm> using namesp…

ubuntu开启ssh服务

更新资源列表&#xff1a;sudo apt-get update -> 输入管理员密码 安装openssh-server: sudo apt-get install openssh-server 查看 ssh服务是否启动&#xff1a;sudo ps -e |grep ssh 启动ssh服务&#xff1a; sudo service ssh start 转载于:https://www.cnblogs.com/ver…

javascript --- 判断只有1个为真

下面写一个用于判断只有一个为真的函数: function onlyOne(a,b,c){return !!((a && !b && !c) ||(!a && b && !c) || (!a && !b && c)); } var a true; var b false;onlyOne(a,b,b) // true onlyOne(a,b,a); // false上述…

13 代码分割之import静动态导入

前端首屏优化方案之一 项目构建时会整体打包成一个bundle的JS文件&#xff0c;而有的代码、模块是加载时不需要的&#xff0c;需要分割出来单独形成一个文件块chunk&#xff08;不会打包在main里&#xff09;&#xff0c;让模块懒加载&#xff08;想加载时才加载&#xff09;&a…

2018.01.01(数字三角形,最长上升子序列等)

2017.12.24 简单的动态规划 1.数字三角形(算法引入) 题目描述&#xff1a;下图所示是一个数字三角形&#xff0c;其中三角形中的数值为正整数&#xff0c;现规定从最顶层往下走到最底层&#xff0c;每一步可沿左斜线向下或右斜线向下走。设三角形有n层&#xff0c;编程计算出从…

Mac iOS 允许从任何来源下载应用并打开

一个快捷的小知识点&#xff0c;mark&#xff01; 允许从任何来源下载应用并打开&#xff0c;不用手动去允许&#xff0c;更加简洁&#xff01; 只需一行命令 sudo spctl --master-disable 1.正常情况下&#xff0c;打开偏好设置&#xff0c;选择安全性与隐私&#xff0c;界面是…

ES5-4 函数基础与种类、形实参及映射、变量类型

模块编程原则&#xff1a;高内聚&#xff0c;低耦合&#xff08;重复部分少&#xff09;&#xff0c;让一个模块有强的功能性、高的独立性 → 单一责任制&#xff0c;用函数进行解耦合。 1. 函数命名规则 不能以数字开头可以以字母_$开头包含数字小驼峰命名法 函数声明一定有…

javascript --- 抽象相等

字符串和数字之间的相等比较 var a 42; var b "42";a b; // false a b; // trueES5规范11.9.3.4-5定义如下: (1)如果Type(x)是数字,Type(y)是字符串,则返回 x ToNumber(y) 的结果 (2)如果Type(x)是字符串,Type(x)是数字,则返回 ToNumber(x) y 的结果// 总结…