This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inherit (SubType, SuperType) { | |
if (Object.create) { | |
var prototype = Object.create(SuperType.prototype); | |
prototype.constructor = SubType; | |
SubType.prototype = prototype; | |
return; | |
} | |
function F() {} | |
F.prototype = SuperType.prototype; | |
var prototype = new F(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="zh-cmn-Hans"> | |
<head> | |
<meta http-equiv="Access-Control-Allow-Origin" content="*"> | |
<meta charset="utf-8"> | |
<meta content="initial-scale=1,maximum-scale=1,user-scalable=no,width=device-width" name="viewport" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> | |
<meta name="renderer" content="webkit"> | |
<meta http-equiv="Cache-Control" content="no-siteapp" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Ajax = (function() { | |
var methodsList = ['get', 'post', 'head', 'options', 'put', 'connect', 'trace', 'delete']; | |
/** | |
* Ajax | |
* @param {String} url | |
* @param {Function} callback | |
* @param {Object/String} data | |
* @param {Object} options | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 事件模拟器与执行器 | |
var eventSimulator = (function () { | |
function fn(element, type) { | |
var ctx = fn; | |
var options = extend(ctx.defaultOptions, arguments[2] || {}); | |
var oEvent, eventType = null; | |
for (var name in ctx.eventMatchers) { | |
if (ctx.eventMatchers[name].test(type)) { | |
eventType = name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventTarget = (function() { | |
var _toArray = (function(obj, offset) { | |
offset = offset >= 0 ? offset : 0; | |
if (Array.from) { | |
// Array.from: convert an obj or an array-like obj to an array | |
return function(obj, offset) { | |
return Array.from(obj).slice(offset); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="zh-cmn-Hans"> | |
<head> | |
<meta http-equiv="Access-Control-Allow-Origin" content="*"> | |
<meta charset="utf-8"> | |
<meta content="initial-scale=1,maximum-scale=1,user-scalable=no,width=device-width" name="viewport" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> | |
<meta name="renderer" content="webkit"> | |
<meta http-equiv="Cache-Control" content="no-siteapp" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 模仿一个需要耗时 2000ms 的异步 API:asyncOp | |
const asyncOp = function(a, b, callback) { | |
try { | |
// 这里是可能出现输出 error 的操作 | |
if (typeof a !== 'number' || typeof b !== 'number') { | |
let err = new Error('asyncOp(): 前两个参数必须为数值'); | |
err.type = 1; | |
throw err; | |
} | |
if (typeof callback !== 'function') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 为 类或构造函数 的原型部署 Iterable 接口 | |
* → 为 类或构造函数 的 prototype 对象部署 [Symbol.iterator] 方法 | |
*/ | |
function Obj(values) { | |
// values 为数组 | |
this.values = values; | |
} | |
// 假设我们的遍历的目标是:遍历对象 values 数组的所有元素值 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* curring 用于为函数减少参数数量 | |
*/ | |
function currying(fn) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
return function() { | |
var innerArgs = Array.prototype.slice.call(arguments); | |
var concatArgs = args.concat(innerArgs); | |
return fn.apply(this, concatArgs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ES6 模块循环依赖时 | |
* 代码的运行过程与 CommonJS 一致 | |
* 不同的是变量引用的处理不同 | |
* ES6加载的变量,都是动态引用其所在的模块。只要引用存在,代码就能执行。 | |
*/ | |
/** | |
* module-a.js | |
*/ |