JavaScript 设计模式 - 工具函数
1、类式继承,模拟面向对象语言的继承方式
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}
2、克隆,实现原型式继承,是 JavaScript 中独有的以对象为蓝本的继承方式
function clone(object) {
var F = function() {};
F.prototype = object;
return new F();
}
3、JavaScript 中模拟面向对象语言接口(Interface)的实现方式
// 首先,声明 Interface 构造函数
var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length
+ " arguments, bue expected exactly 2.");
}
this.name = name;
this.methods = [];
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i] !== 'string') {
throw new Error("Interface constructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
}
// 为 Interface 定义一个类方法,用于检测 object 对象是否实现了指定接口的方法?
Interface.ensureImplements = function(object) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with "
+ arguments.length + " arguments, bue expected at least 2.");
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments "
+ "two and above to be instances of Interface.");
}
for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j];
if(!object[method] || typeof method !== 'function') {
throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + interface.name
+ " interface. Method " + method + " was not found.");
}
}
}
};
/// 以下为示例 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// 声明接口,只需声明,无需实现
var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);
// 声明接口,只需声明,无需实现
var FormItem = new Interface('FormItem', ['save']);
// 声明实现的类
function MenuItem(id, method, action) {
...
this.show = function(object) {
Interface.ensureImplements(this, [Composite, MenuItem]);
// 保证类对象实现了 Composite, MenuItem 接口,才能执行下面的逻辑
...
}
...
}
JavaScript 设计模式 - 工具函数的更多相关文章
- javascript 实用工具函数
整理日常开发中我们常常会使用到的一些工具函数. var utils = (function(){ var fay = {}; // 返回当前时间的毫秒数 fay.getTime = Date.now( ...
- javascript常用工具函数总结(不定期补充)未指定标题的文章
前言 以下代码来自:自己写的.工作项目框架上用到的.其他框架源码上的.网上看到的. 主要是作为工具函数,服务于框架业务,自身不依赖于其他框架类库,部分使用到es6/es7的语法使用时要注意转码 虽然尽 ...
- JavaScript常用工具函数
检测数据是不是除了symbol外的原始数据 function isStatic(value) { return ( typeof value === 'string' || typeof value ...
- JavaScript设计模式-1.函数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- javascript工具函数
第一部分 JavaScript工具函数 转义特殊字符为html实体 HtmlEncode: function(str){ return str.replace(/&/g, '&') ...
- JavaScript设计模式与开发实践——读书笔记1.高阶函数(上)
说来惭愧,4个多月未更新了.4月份以后就开始忙起来了,论文.毕设.毕业旅行等七七八八的事情占据了很多时间,毕业之后开始忙碌的工作,这期间一直想写博客,但是一直没能静下心写.这段时间在看<Java ...
- JavaScript封装Ajax工具函数及jQuery中的ajax,xhr在IE的兼容
封装ajax工具函数 首先要思考:1.为什么要封装它?提高开发效率2.把一些不确定的情况考虑在其中 a. 请求方式 b. 请求地址 c. 是否异步 d. 发送参数 e. 成功处理 f. 失败处理3.确 ...
- 常用的工具函数助力JavaScript高效开发
前言 日常开发中,面对各种不同的需求,我们经常会用到以前开发过的一些工具函数,把这些工具函数收集起来,将大大提高我们的开发效率. 1.校验数据类型 export const typeOf = func ...
- 【读书笔记】读《JavaScript设计模式》之装饰者模式
一.定义 装饰者模式可用来透明地把对象包装在具有同样接口的另一个对象之中.这样一来,你可以给一个方法添加一些行为,然后将方法调用传递给原始对象.相对于创建子类来说,使用装饰者对象是一种更灵活的选择(装 ...
随机推荐
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- RedHat5--yun源无法使用问题解决
YUM是Redhat Linux在线安装更新及软件的工具,但是这是RHEL5的收费功能,如果没有购买Redhat的服务时不能使用RHEL5的更新源的,会提示注册. 由于CentOS是从Redhat演化 ...
- gps转百度地图
HttpResponse res=WS.url(mapUrl+"/ag/coord/convert?from=0&to=4&x="+longitude+" ...
- 2016-08-15: C++ traits
#include <stdio.h> template <typename T> struct TraitsHelper { static const bool isPoint ...
- 一致性hash介绍
像Memcache以及其它一些内存K/V数据库一样,Redis本身不提供分布式支持,所以在部署多台Redis服务器时,就需要解决如何把数据分散到各个服务器的问题,并且在服务器数量变化时,能做到最大程度 ...
- Deep Learning(深度学习)学习笔记整理(二)
本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep learning简介 [2]Deep Learning训练过程 [3]Deep Learning模型之 ...
- nginx 启动,停止和重新加载配置
要启动nginx的,运行可执行文件.一旦nginx的启动时,它可以通过与-s参数调用可执行来控制.使用以下语法 nginx -s signal 其中,信号可以是下列之一: stop - fast sh ...
- ---ps 命令
ps 的命令真复杂啊! 值得注意的是选项的 -e e 这两个是不同的 -e的选项和x的意思相当 而e 的意思是改变显示栏目内容了 我个人用单字母的比较多 ps f -eo pid,uid,gid,us ...
- 8.4 H5知识点总结
HTML简介 HyperText Markup Language 简称为HTML HyperText: 超文本 (文本 + 图片 + 视频 + 音频 + 链接) Markup Language: 标记 ...
- 为窗体设置背景图片-UI界面编辑器(SkinStudio)教程
1.1. 为窗体设置背景图片 在窗体的Background属性中选择图片设置为窗体背景图片