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设计模式》之装饰者模式
一.定义 装饰者模式可用来透明地把对象包装在具有同样接口的另一个对象之中.这样一来,你可以给一个方法添加一些行为,然后将方法调用传递给原始对象.相对于创建子类来说,使用装饰者对象是一种更灵活的选择(装 ...
随机推荐
- SourceInsight 精确导入Linux kernel源码的方法
相信有很多人用 SourceInsight 查看 Linux Kernel 源码,但导入源码时会遇到一些问题.1.如果把整个源码树都导入进去,查看符号定义的时候,会发现有大量重复定义,很难找到正确的位 ...
- Debian 7.6 新编译内核 3.15.6 开机加载黑屏
需要手动加载 fbcon 这个模块,或者编译内核的时候,Framebuffer Console support 编译进内核(后者没测试过).加在模块只要修改/etc/default/grub文件或者/ ...
- TFS 2010 如何删除Collection
在cmd 中 cd 到 目录 c:\Program Files\Microsoft Team Foundation Sever 2010\Tools 执行下面的命令: TfsConfig colle ...
- No.014 Longest Common Prefix
14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...
- 返回记录结构时,如果需要返回为nil时 应该怎么办。
- I can connect to an FTP site but I can't list or transfer files.
原文 FTP sessions use two network connections: The control channel is for user authentication and send ...
- Asp.net Page指令
Page指令为编译器编译页面时使用的指令 指令的格式为 <%@ [Directive] [Attribute=value]%> <%@ Page Language="C#& ...
- Add Binary <leetcode>
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- hdu 2196 computer
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- swift 获取控件位置 大小
var SearchBtn = uibutton() SearchBtn.frame.origin.x //获取坐标x SearchBtn.frame.origin.Y // 获取坐标Y Sea ...