• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >JavaScript > javascript Module

javascript Module

作者:monica12的专栏 字体:[增加 减小] 来源:互联网 时间:2017-08-14

monica12的专栏通过本文主要向大家介绍了javascript Module等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

  模块=函数+闭包构成,一个模块是用一个对象或函数来表示的一个接口,并隐藏了属性和方法。通过使用函数去产生模块,我们可以完全不用全局变量,因此可以缓解JavaScript’s 最糟糕的特征之一。
例如:假设我们想在String对象增加一个 deentityify()方法 它的工作在HTML里寻找在字符串里的实体,然后用对应的值替换他们,在一个对象里,保存实体名字和他们对应的值,是有意义。但是我们把对象保存在哪里呢?我们要放在一个全局变量里,但是全局变量不好,我们能放在函数里面?但是有运行时成本,每当函数被调用时固定值必须被计算,理想的方法是放到闭包里,也许提供一特别的方法增加实体。

    

String.method('deentityify', function ( ) {
// The entity table. It maps entity names to
// characters.
var entity = {
quot: '"',

lt: '<',
gt: '>'
};
// Return the deentityify method.
return function ( ) {
// This is the deentityify method. It calls the string
// replace method, looking for substrings that start
// with '&' and end with ';'. If the characters in
// between are in the entity table, then replace the
// entity with the character from the table. It uses
// a regular expression (Chapter 7).
return this.replace(/&([^&;]+);/g,
function (a, b) {
var r = entity[b];
return typeof r === 'string' ? r : a;
}
);
};
}());



 

 

Notice the last line. We immediately invoke the function we just made with the( )
operator. That invocation creates and returns the function that becomes the
deentityify method.

 

 

document.writeln(
       '<">'.deentityify( )); // <">


The module pattern takes advantage of function scope and closure to create relationships that are binding and private. In this example, only thedeentityify method has
access to the entity data structure.
The general pattern of a module is a function that defines private variables and functions; creates privileged functions which, through closure, will have access to the private variables and functions; and that returns the privileged functions or stores them  in an accessible place.

 

Use of the module pattern can eliminate the use of global variables. It promotes
information hiding and other good design practices. It is very effective in encapsulating applications and other singletons.
It can also be used to produce objects that are secure. Let’s suppose we want to make
an object that produces a serial number:

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2017-05-11微信小程序左右滑动切换页面详解及实例代码
  • 2017-05-11简单好用的nodejs 爬虫框架分享
  • 2017-05-11使用jquery datatable和bootsrap创建表格实例代码
  • 2017-05-11Bootstrap modal 多弹窗之叠加显示不出弹窗问题的解决方案
  • 2017-05-11详解vue组件化开发-vuex状态管理库
  • 2017-05-11javascript 中设置window.location.href跳转无效问题解决办法
  • 2017-05-11Javascript ES6中数据类型Symbol的使用详解
  • 2017-05-11JS实现css hover操作的方法示例
  • 2017-08-21JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
  • 2017-05-11js鼠标经过tab选项卡时实现切换延迟

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 详解vue.js全局组件和局部组件
    • JS实现标签页切换效果
    • Nodejs进阶:express+session实现简易登录身份认证
    • JS控件bootstrap datepicker使用方法详解
    • 微信小程序开发之相册选择和拍照详解及实例代码
    • jQuery倒计时代码(超简单)
    • javascript实现数据双向绑定的三种方式小结
    • 微信小程序 点击控件后选中其它反选实例详解
    • vue.js之vue-cli脚手架的搭建详解
    • BootStrap与Select2使用小结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有