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 设计模式 - 工具函数的更多相关文章

  1. javascript 实用工具函数

    整理日常开发中我们常常会使用到的一些工具函数. var utils = (function(){ var fay = {}; // 返回当前时间的毫秒数 fay.getTime = Date.now( ...

  2. javascript常用工具函数总结(不定期补充)未指定标题的文章

    前言 以下代码来自:自己写的.工作项目框架上用到的.其他框架源码上的.网上看到的. 主要是作为工具函数,服务于框架业务,自身不依赖于其他框架类库,部分使用到es6/es7的语法使用时要注意转码 虽然尽 ...

  3. JavaScript常用工具函数

    检测数据是不是除了symbol外的原始数据 function isStatic(value) { return ( typeof value === 'string' || typeof value ...

  4. JavaScript设计模式-1.函数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. javascript工具函数

    第一部分 JavaScript工具函数 转义特殊字符为html实体   HtmlEncode: function(str){ return str.replace(/&/g, '&') ...

  6. JavaScript设计模式与开发实践——读书笔记1.高阶函数(上)

    说来惭愧,4个多月未更新了.4月份以后就开始忙起来了,论文.毕设.毕业旅行等七七八八的事情占据了很多时间,毕业之后开始忙碌的工作,这期间一直想写博客,但是一直没能静下心写.这段时间在看<Java ...

  7. JavaScript封装Ajax工具函数及jQuery中的ajax,xhr在IE的兼容

    封装ajax工具函数 首先要思考:1.为什么要封装它?提高开发效率2.把一些不确定的情况考虑在其中 a. 请求方式 b. 请求地址 c. 是否异步 d. 发送参数 e. 成功处理 f. 失败处理3.确 ...

  8. 常用的工具函数助力JavaScript高效开发

    前言 日常开发中,面对各种不同的需求,我们经常会用到以前开发过的一些工具函数,把这些工具函数收集起来,将大大提高我们的开发效率. 1.校验数据类型 export const typeOf = func ...

  9. 【读书笔记】读《JavaScript设计模式》之装饰者模式

    一.定义 装饰者模式可用来透明地把对象包装在具有同样接口的另一个对象之中.这样一来,你可以给一个方法添加一些行为,然后将方法调用传递给原始对象.相对于创建子类来说,使用装饰者对象是一种更灵活的选择(装 ...

随机推荐

  1. eclipse中提示HttpServletRequest不能引用的解决办法

    两种解决方法: 1.右键点击项目->Build Path->Add Libraries..->Server Runtime 选择Apache Tomcat v8.0 2.右键点击项目 ...

  2. SSH框架流程

    流程图 具体步骤 一.实体类 //Serializable在网络的环境下做类传输public class Category implements Serializable { private Inte ...

  3. 【VB.NET】类绑定控件,实现文本框快捷键全选

    Public Class KeyBinder Public Sub BindControl(ByRef CControl As TextBox) AddHandler CControl.KeyDown ...

  4. windows与OSX双操的时区-黑苹果之路

    问题由来已久,原因好像是windows识别时间的方式跟OSX不一样,方法如下: 1,改苹果系统时区为冰岛 2,改window系统的注册表 在管理员cmd下运行 Reg add HKLM\SYSTEM\ ...

  5. 修复山寨版的J-Link

    Fixed J-Link 1. Erase   (1) Power On   (2) Jump "ERASE"(JP3)   (3) Wait for 5s   (4) Break ...

  6. Java正则表达式详解

    转自http://edu.yesky.com/edupxpt/18/2143018.shtml

  7. [转载]:fortran之format格式化输出总结

    先贴一段别人总结好的: 格式化输出的控制字符非常的丰富,但常用的并不多,一般说来:" I .F.E.A.X "是最常使用的几个格式,最 好把它们都记下来. Iw[.m] 以w个字符 ...

  8. Ms sql将首字母大写

    --辅助表 create table a ( a int ) declare @b int begin insert into a values(@b) end; go --表数据 ),id int) ...

  9. 树莓派B+上手小记--使用HDMI线连接显示器

    入手还算比较顺利,一开始使用网上下的别人精简的OS,发现ACT及PWR灯一直亮着,上网查说用HDMI连接显示器需要修改配置文件config.txt,但修改后情况依旧. 如果还是用官方的系统试试吧,上网 ...

  10. Solr Cloud搭建

    1:搭建tomcat 配置connector: server.xm文件中: <Connector port="8080"maxThreads="200" ...