This file contains hidden or 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
// @param {Boolean} defaults 默认不传参,为undefined,目标对象的已有属性不被覆盖;为true,则可以被覆盖 | |
function createAssigner(defaults) { | |
function extend(obj) { | |
for (var i = 1; i < arguments.length; i++) { | |
var def = arguments[i]; | |
for (var key in def) { | |
if (!defaults || obj[key] === void 0) { | |
obj[key] = def[key]; | |
} |
This file contains hidden or 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
/** | |
* @param {Function} subType | |
* @param {Function} superType | |
*/ | |
function inheritPrototype(subType, superType) { | |
// 以一个空函数作为中介,保证不把父类构造器中的属性带进原型链 | |
function F() {} | |
// 把F的prototype指向父类的prototype,修改整个prototype而不是部分prototype | |
F.prototype = superType.prototype; | |
// new F()完成两件事情,1. 执行F构造函数,为空;2. 执行F的prototype的内存分配,这里就是父类,也就是Person的getAge方法 |
This file contains hidden or 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
// example 1 | |
function a(c) { | |
// 在a的作用域中声明b | |
return function b(d) { | |
// b中使用了a的局部变量c | |
c += 1; | |
return c + d; | |
} | |
} |
This file contains hidden or 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
/** | |
* @param {String} downloadUri | |
* @param {Boolean} useIFrame | |
*/ | |
function startDownloading(downloadUri, useIFrame) { | |
if (!downloadUri || !downloadUri.length) { | |
return; | |
} | |
if (useIFrame === void 0) { |
This file contains hidden or 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
// JS 浮点数四则运算精度丢失解决方案 | |
// 计算小数点后面有几位 | |
function getBits(num) { | |
var eq1; | |
return (eq1 = num.toString().split('.')[1]) ? eq1.length : 0; | |
} | |
// 加法 | |
Number.prototype.add = function(arg) { |
This file contains hidden or 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
/** | |
* @param {String} substr 用来搜索的子串 | |
* @param {String} str 被搜索的字符串 | |
* @return {Array} 由匹配到的索引组成的数组 | |
*/ | |
function getIndexes(substr, str) { | |
var arr = []; | |
var index; | |
var len = 0; | |
var slicedStr = str; |
This file contains hidden or 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 createClass = function (Parent, props) { | |
if (arguments.length === 1) { | |
props = Parent; | |
Parent = null; | |
} | |
var Child, F, i; | |
// 1. 构造函数 | |
Child = function Klass() { |
This file contains hidden or 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 B = {}; | |
B.url = {}; | |
// 获取search 或 hash 中的参数 | |
B.url.getParam = function(name, url) { | |
url = url || location.href; | |
var reg = new RegExp("(^|&|\\?|#)" + name + "=([^&]*?)(&|#|$)"); | |
var tempHash = url.match(/#.*/) ? url.match(/#.*/)[0] : ""; | |
url = url.replace(/#.*/, ""); | |
if (reg.test(tempHash)) { |
This file contains hidden or 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
/** | |
* 1. Original JS Implementation by Jeff Greenberg 2/2001 - http://home.earthlink.net/~kendrasg/info/js_opt/ | |
* 2. (fast duff's device) from an anonymous donor to Jeff Greenberg's site | |
* 3. (faster duff's defice) by Andrew King 8/2002 for WebSiteOptimization.com | |
* 4. bug fix (for iterations<8) by Andrew B. King April 12, 2003 | |
*/ | |
function duffsDevice (iterations) { | |
var testVal = 0, | |
n = iterations % 8; |