作者: 铁锚

日期: 2013年12月19日

说明: 本系列文章为草稿,等待后期完善。源码是jQuery版本的,code.photoswipe-3.0.5.js

1. 代码开头,就是一些版权申明,没什么好说的,MIT授权。

// Copyright (c) 2012 by Code Computerlove (http://www.codecomputerlove.com)
// Licensed under the MIT license
// version: 3.0.5

2. 接下来的代码段,是一种闭包形式,匿名空间式的代码段,第一段代码的格式如下:

(function (window) {
  // 代码区域
  // Function.prototype.bind 函数绑定 部分
  // window.Code.Util 部分
}(window));

说明: 如果要在自己的代码环境中将 window 指代为全局window,只有采用函数参数的形式,如果 直接 var window = xxx;将会报语法错误。

3. 对 Function 函数定义类的hack,如果没有bind原型方法则用自定义的方法来实现:

// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
if (!Function.prototype.bind ) {
  Function.prototype.bind = function( obj ) {
    var slice = [].slice,
        args = slice.call(arguments, 1),
        self = this,
        nop = function () {},
        bound = function () {
          return self.apply( this instanceof nop ? this : ( obj || {} ),
                              args.concat( slice.call(arguments) ) );
        };

    nop.prototype = self.prototype;
    bound.prototype = new nop();
    return bound;
  };
}

4. 接下来是window.Code工具类的封装:

  if (typeof window.Code === "undefined") {
    window.Code = {};
  }

  window.Code.Util = {
    /*
     * Function: registerNamespace
     */
    registerNamespace: function () {
      var
        args = arguments, obj = null, i, j, ns, nsParts, root, argsLen, nsPartsLens;
      for (i=0, argsLen=args.length; i<argsLen; i++) {
        ns = args[i];
        nsParts = ns.split(".");
        root = nsParts[0];
        if (typeof window[root] === "undefined"){
          window[root] = {};
        }
        obj = window[root];
        //eval('if (typeof ' + root + ' == "undefined"){' + root + ' = {};} obj = ' + root + ';');
        for (j=1, nsPartsLens=nsParts.length; j<nsPartsLens; ++j) {
          obj[nsParts[j]] = obj[nsParts[j]] || {};
          obj = obj[nsParts[j]];
        }
      }
    },
    /*
     * Function: coalesce
     * Takes any number of arguments and returns the first non Null / Undefined argument.
     */
    coalesce: function () {
      var i, j;
      for (i=0, j=arguments.length; i<j; i++) {
        if (!this.isNothing(arguments[i])) {
          return arguments[i];
        }
      }
      return null;
    },
    /*
     * Function: extend
     */
    extend: function(destination, source, overwriteProperties){
      var prop;
      if (this.isNothing(overwriteProperties)){
        overwriteProperties = true;
      }
      if (destination && source && this.isObject(source)){
        for(prop in source){
          if (this.objectHasProperty(source, prop)) {
            if (overwriteProperties){
              destination[prop] = source[prop];
            }
            else{
              if(typeof destination[prop] === "undefined"){
                destination[prop] = source[prop];
              }
            }
          }
        }
      }
    },
    /*
     * Function: clone
     */
    clone: function(obj) {
      var retval = {};
      this.extend(retval, obj);
      return retval;
    },
    /*
     * Function: isObject
     */
    isObject: function(obj){
      return obj instanceof Object;
    },
    /*
     * Function: isFunction
     */
    isFunction: function(obj){
      return ({}).toString.call(obj) === "[object Function]";
    },
    /*
     * Function: isArray
     */
    isArray: function(obj){
      return obj instanceof Array;
    },
    /*
     * Function: isLikeArray
     */
    isLikeArray: function(obj) {
      return typeof obj.length === 'number';
    },
    /*
     * Function: isNumber
     */
    isNumber: function(obj){
      return typeof obj === "number";
    },
    /*
     * Function: isString
     */
    isString: function(obj){
      return typeof obj === "string";
    },
    /*
     * Function: isNothing
     */
    isNothing: function (obj) {

      if (typeof obj === "undefined" || obj === null) {
        return true;
      }
      return false;

    },
    /*
     * Function: swapArrayElements
     */
    swapArrayElements: function(arr, i, j){

      var temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;

    },
    /*
     * Function: trim
     */
    trim: function(val) {
      return val.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    },
    /*
     * Function: toCamelCase
     */
    toCamelCase: function(val){
      return val.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');});
    },
    /*
     * Function: toDashedCase
     */
    toDashedCase: function(val){
      return val.replace(/([A-Z])/g, function($1){return "-"+$1.toLowerCase();});
    },
    /*
     * Function: indexOf
     */
    arrayIndexOf: function(obj, array, prop){
      var i, j, retval, arrayItem;
      retval = -1;
      for (i=0, j=array.length; i<j; i++){
        arrayItem = array[i];

        if (!this.isNothing(prop)){
          if (this.objectHasProperty(arrayItem, prop)) {
            if (arrayItem[prop] === obj){
              retval = i;
              break;
            }
          }
        }
        else{
          if (arrayItem === obj){
            retval = i;
            break;
          }
        }
      }
      return retval;
    },
    /*
     * Function: objectHasProperty
     */
    objectHasProperty: function(obj, propName){

      if (obj.hasOwnProperty){
        return obj.hasOwnProperty(propName);
      }
      else{
        return ('undefined' !== typeof obj[propName]);
      }
    }
  };

内部是一些需要使用到的工具函数.

PhotoSwipe源码解读系列(二)的更多相关文章

  1. swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?

    date: 2018-8-01 14:22:17title: swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?description: 阅读 sowft 框架源码, 了解 sowf ...

  2. Alamofire源码解读系列(二)之错误处理(AFError)

    本篇主要讲解Alamofire中错误的处理机制 前言 在开发中,往往最容易被忽略的内容就是对错误的处理.有经验的开发者,能够对自己写的每行代码负责,而且非常清楚自己写的代码在什么时候会出现异常,这样就 ...

  3. Alamofire源码解读系列(十二)之时间轴(Timeline)

    本篇带来Alamofire中关于Timeline的一些思路 前言 Timeline翻译后的意思是时间轴,可以表示一个事件从开始到结束的时间节点.时间轴的概念能够应用在很多地方,比如说微博的主页就是一个 ...

  4. Alamofire源码解读系列(十二)之请求(Request)

    本篇是Alamofire中的请求抽象层的讲解 前言 在Alamofire中,围绕着Request,设计了很多额外的特性,这也恰恰表明,Request是所有请求的基础部分和发起点.这无疑给我们一个Req ...

  5. Alamofire源码解读系列(四)之参数编码(ParameterEncoding)

    本篇讲解参数编码的内容 前言 我们在开发中发的每一个请求都是通过URLRequest来进行封装的,可以通过一个URL生成URLRequest.那么如果我有一个参数字典,这个参数字典又是如何从客户端传递 ...

  6. Alamofire源码解读系列(三)之通知处理(Notification)

    本篇讲解swift中通知的用法 前言 通知作为传递事件和数据的载体,在使用中是不受限制的.由于忘记移除某个通知的监听,会造成很多潜在的问题,这些问题在测试中是很难被发现的.但这不是我们这篇文章探讨的主 ...

  7. Alamofire源码解读系列(五)之结果封装(Result)

    本篇讲解Result的封装 前言 有时候,我们会根据现实中的事物来对程序中的某个业务关系进行抽象,这句话很难理解.在Alamofire中,使用Response来描述请求后的结果.我们都知道Alamof ...

  8. Alamofire源码解读系列(六)之Task代理(TaskDelegate)

    本篇介绍Task代理(TaskDelegate.swift) 前言 我相信可能有80%的同学使用AFNetworking或者Alamofire处理网络事件,并且这两个框架都提供了丰富的功能,我也相信很 ...

  9. Alamofire源码解读系列(七)之网络监控(NetworkReachabilityManager)

    Alamofire源码解读系列(七)之网络监控(NetworkReachabilityManager) 本篇主要讲解iOS开发中的网络监控 前言 在开发中,有时候我们需要获取这些信息: 手机是否联网 ...

随机推荐

  1. Node.js 工具模块

    在 Node.js 模块库中有很多好用的模块.接下来我们为大家介绍几种常用模块的使用: 序号 模块名 & 描述 1 OS 模块 提供基本的系统操作函数. 2 Path 模块提供了处理和转换文件 ...

  2. Android简易实战教程--第四十四话《ScrollView和HorizontalScrollView简单使用》

    一.ScrollView 由于手机屏幕的高度有限,当普通布局放不下现实和的内容时,ScrollView视图(滚动视图)就会派上用场,因为数据可以往下滚动显示. 二.HorizontalScrollVi ...

  3. 如何将一个二进制的xxx.bin文件轻松转为C语言数组

    今天在写一个SPI-flash读写程序,目的是要将一个二进制文件写到SPI_FLASH中,最后通过开机读取,实际上这个.bin文件就是uboot和second-boot的结合体.通过SD卡写到SPI- ...

  4. [转]django-registration quickstart

    Basic configuration and use--------------------------- Once installed, you can add django-registrati ...

  5. Java使用BigDecimal保留double、float运算精度、保留指定位数有效数字、四舍五入

    工具类 package --; import java.math.BigDecimal; /** * Created by kongqw on 2015/12/10. */ public final ...

  6. JAVA对象及属性的内存堆栈管理(通过小程序简单说明)

    JAVA在执行过程中会划分4个内存区域(heap.stack.data segment.code segment)代码区(codesegment):java开始执行会把代码加载到code segmen ...

  7. 在ubuntu上部署一个samba服务器

    今天公司装了一天新电脑,准备把它装成服务器,于是为了方便开发的使用,我在上面部署了一个samba,用来实现window和linux的联系: 具体步骤,我详细的查看了百度,高手云集,以下就是总结了网友的 ...

  8. android问题:Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/24196143 Installation error: INSTALL_FAILED_C ...

  9. (一〇八)iPad开发之横竖屏适配

    在iPad开发中,横竖屏的视图常常是不同的,例如侧边栏Dock,在横屏时用于屏幕较宽,可以展示足够多的内容,每个按钮都可以展示出标题:而竖屏时Dock应该比较窄,只显示图标不现实按钮标题. iPad比 ...

  10. 【一天一道LeetCode】#350. Intersection of Two Arrays II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...