PhotoSwipe源码解读系列(二)

简介: 作者: 铁锚 日期: 2013年12月19日 说明: 本系列文章为草稿,等待后期完善。源码是jQuery版本的,code.photoswipe-3.0.5.js 1. 代码开头,就是一些版权申明,没什么好说的,MIT授权。

作者: 铁锚

日期: 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]);
      }
    }
  };
内部是一些需要使用到的工具函数.



目录
相关文章
|
24天前
|
存储 负载均衡 API
服务发现原理分析与源码解读
服务发现原理分析与源码解读
|
1月前
|
存储 前端开发 JavaScript
EaselJS 源码分析系列--第四篇
EaselJS 源码分析系列--第四篇
|
4月前
|
Java API 开发者
springboot 多线程的使用原理与实战
在Spring Boot中实现多线程,主要依赖于Spring框架的@Async注解以及底层Java的并发框架。这里将深入剖析Spring Boot多线程的原理,包括@Async注解的工作方式、任务执行器的角色以及如何通过配置来调整线程行为。
454 5
|
Java Spring 容器
【框架源码】SpringBoot核心源码解读之启动类源码分析
【框架源码】SpringBoot核心源码解读之启动类源码分析
【框架源码】SpringBoot核心源码解读之启动类源码分析
|
缓存 分布式计算 监控
【源码解读】| LiveListenerBus源码解读(上)
【源码解读】| LiveListenerBus源码解读
162 0
【源码解读】| LiveListenerBus源码解读(上)
|
存储 SQL 分布式计算
【源码解读】| LiveListenerBus源码解读(下)
【源码解读】| LiveListenerBus源码解读
149 0
【源码解读】| LiveListenerBus源码解读(下)
|
存储 分布式计算 监控
【源码解读】|SparkEnv源码解读
【源码解读】|SparkEnv源码解读
128 0
|
存储
HashMap源码解读(下篇)
HashMap源码解读(下篇)
96 0
HashMap源码解读(下篇)
|
存储 Java 索引
HashMap源码解读(中篇)
HashMap源码解读(中篇)
100 0
HashMap源码解读(中篇)
|
存储 Java 对象存储
HashMap源码解读(上篇)
HashMap源码解读(上篇)
114 0
HashMap源码解读(上篇)