PhotoSwipe源码解读系列(二)
作者: 铁锚
日期: 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源码解读系列(二)的更多相关文章
- swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?
date: 2018-8-01 14:22:17title: swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?description: 阅读 sowft 框架源码, 了解 sowf ...
- Alamofire源码解读系列(二)之错误处理(AFError)
本篇主要讲解Alamofire中错误的处理机制 前言 在开发中,往往最容易被忽略的内容就是对错误的处理.有经验的开发者,能够对自己写的每行代码负责,而且非常清楚自己写的代码在什么时候会出现异常,这样就 ...
- Alamofire源码解读系列(十二)之时间轴(Timeline)
本篇带来Alamofire中关于Timeline的一些思路 前言 Timeline翻译后的意思是时间轴,可以表示一个事件从开始到结束的时间节点.时间轴的概念能够应用在很多地方,比如说微博的主页就是一个 ...
- Alamofire源码解读系列(十二)之请求(Request)
本篇是Alamofire中的请求抽象层的讲解 前言 在Alamofire中,围绕着Request,设计了很多额外的特性,这也恰恰表明,Request是所有请求的基础部分和发起点.这无疑给我们一个Req ...
- Alamofire源码解读系列(四)之参数编码(ParameterEncoding)
本篇讲解参数编码的内容 前言 我们在开发中发的每一个请求都是通过URLRequest来进行封装的,可以通过一个URL生成URLRequest.那么如果我有一个参数字典,这个参数字典又是如何从客户端传递 ...
- Alamofire源码解读系列(三)之通知处理(Notification)
本篇讲解swift中通知的用法 前言 通知作为传递事件和数据的载体,在使用中是不受限制的.由于忘记移除某个通知的监听,会造成很多潜在的问题,这些问题在测试中是很难被发现的.但这不是我们这篇文章探讨的主 ...
- Alamofire源码解读系列(五)之结果封装(Result)
本篇讲解Result的封装 前言 有时候,我们会根据现实中的事物来对程序中的某个业务关系进行抽象,这句话很难理解.在Alamofire中,使用Response来描述请求后的结果.我们都知道Alamof ...
- Alamofire源码解读系列(六)之Task代理(TaskDelegate)
本篇介绍Task代理(TaskDelegate.swift) 前言 我相信可能有80%的同学使用AFNetworking或者Alamofire处理网络事件,并且这两个框架都提供了丰富的功能,我也相信很 ...
- Alamofire源码解读系列(七)之网络监控(NetworkReachabilityManager)
Alamofire源码解读系列(七)之网络监控(NetworkReachabilityManager) 本篇主要讲解iOS开发中的网络监控 前言 在开发中,有时候我们需要获取这些信息: 手机是否联网 ...
随机推荐
- Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...
- Go 语言结构
Go Hello World 实例 Go 语言的基础组成有以下几个部分: 包声明 引入包 函数 变量 语句 & 表达式 注释 接下来让我们来看下简单的代码,该代码输出了"Hello ...
- PHP $_GET 变量
$_GET 变量 预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值. 从带有 GET 方法的表单发送的信息,对任何人都是可见的(会显示在浏览器的地址栏 ...
- MongoDB 查询分析
MongoDB 查询分析可以确保我们建议的索引是否有效,是查询语句性能分析的重要工具. MongoDB 查询分析常用函数有:explain() 和 hint(). 使用 explain() expla ...
- Docker 控制组
控制组(cgroups)是 Linux 内核的一个特性,主要用来对共享资源进行隔离.限制.审计等.只有能控制分配到容器的资源,才能避免当多个容器同时运行时的对系统资源的竞争. 控制组技术最早是由 Go ...
- 虚拟机访问互联网的方法 -- 以RedHat系为例
在虚拟机的三种网络模式中(Host-Only.桥接.NAT),能够实现虚拟机访问互联网的只有桥接与NAT模式,而Host-only主能实现虚拟机与主机两者间的通信.下面以RedHat系虚拟机系统为例, ...
- 【移动开发】targetSdkVersion的作用
在AndroidMenifest.xml中,常常会有下面的语句: <uses-sdk android:minSdkVersion="4" android:targetSdkV ...
- (Java)微信之个人公众账号开发(一)——进入开发者模式
本篇文章将教大家如何建立微信个人公众账号,(注意:后台全部是用javaweb相关技术开发),大家知道,现在微信公众账号分服务号和订阅号,现在我要讲的主要是个人微信公众账号的建立以及后台的开发,个人公众 ...
- norflash芯片内执行(XIP)
为什么程序不能直接在nandflash上执行?出于这个疑惑带来了这篇博文,是我在网上找了很多资料后总结的,假如有误,希望马上指出来,免得我误人子弟.谢谢! nandflash和norflash NOR ...
- activiti 多实例任务
1.1.1. 前言 个人,那么当5个人都投票的时候大概分为如下几种: 1.部门所有人都去投票,当所有人都投票完成的时候,这个节点结束,流程运转到下一个节点.(所有的人都需要投票) 2.部门所有人都去投 ...