Last active
December 20, 2015 03:19
-
-
Save 6174/6062857 to your computer and use it in GitHub Desktop.
ecma262 v5语法补丁
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
/********************************************************************* | |
* ecma262 v5语法补丁 * | |
**********************************************************************/ | |
//-_- 这个trim 测试, 我是不是要 "xuejia".trim | |
if (!"司徒正美".trim) { | |
String.prototype.trim = function() { | |
return this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, '') | |
} | |
} | |
avalon.mix = avalon.fn.mix = function() { | |
var options, name, src, copy, copyIsArray, clone, | |
target = arguments[0] || {}, | |
i = 1, | |
length = arguments.length, | |
deep = false; | |
// 如果第一个参数为布尔,判定是否深拷贝 | |
if (typeof target === "boolean") { | |
deep = target | |
target = arguments[1] || {} | |
i = 2 | |
} | |
//确保接受方为一个复杂的数据类型 | |
if (typeof target !== "object" && getType(target) !== "function") { | |
target = {} | |
} | |
//如果只有一个参数,那么新成员添加于mix所在的对象上 | |
if (length === i) { | |
target = this | |
--i | |
} | |
for (; i < length; i++) { | |
//只处理非空参数 | |
if ((options = arguments[i]) != null) { | |
for (name in options) { | |
src = target[name] | |
copy = options[name] | |
// 防止环引用 | |
if (target === copy) { | |
continue | |
} | |
if (deep && copy && (avalon.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) { | |
if (copyIsArray) { | |
copyIsArray = false | |
clone = src && Array.isArray(src) ? src : [] | |
} else { | |
clone = src && avalon.isPlainObject(src) ? src : {} | |
} | |
target[name] = avalon.mix(deep, clone, copy) | |
} else if (copy !== void 0) { | |
target[name] = copy | |
} | |
} | |
} | |
} | |
return target | |
} | |
function getType(obj) { //取得类型 | |
if (obj == null) { | |
return String(obj) | |
} | |
// 早期的webkit内核浏览器实现了已废弃的ecma262v4标准,可以将正则字面量当作函数使用,因此typeof在判定正则时会返回function | |
return typeof obj === "object" || typeof obj === "function" ? | |
class2type[serialize.call(obj)] || "object" : | |
typeof obj | |
} | |
//var DONT_ENUM = "propertyIsEnumerable,isPrototypeOf,hasOwnProperty,toLocaleString,toString,valueOf,constructor".split(",") | |
for (var i in { | |
toString: 1 | |
}) { | |
DONT_ENUM = false | |
} | |
if (!Object.keys) { | |
Object.keys = function(obj) { //ecma262v5 15.2.3.14 | |
var result = [] | |
for (var key in obj) | |
if (obj.hasOwnProperty(key)) { | |
result.push(key) | |
} | |
if (DONT_ENUM && obj) { | |
for (var i = 0; key = DONT_ENUM[i++];) { | |
if (obj.hasOwnProperty(key)) { | |
result.push(key) | |
} | |
} | |
} | |
return result | |
} | |
} | |
if (!Array.isArray) { | |
Array.isArray = function(a) { | |
return getType(a) === "array" | |
} | |
} | |
if (!noop.bind) { | |
Function.prototype.bind = function() { | |
var fn = this, | |
args = [] | |
args.push.apply(args, arguments) | |
return function() { | |
args.push.apply(args, arguments) | |
var scope = args.shift(); | |
return fn.apply(scope, args) | |
} | |
} | |
} | |
function iterator(vars, body, ret) { | |
var fun = 'for(var ' + vars + 'i=0,n = this.length; i < n; i++){' + body.replace('_', '((i in this) && fn.call(scope,this[i],i,this))') + '}' + ret | |
return Function("fn,scope", fun) | |
} | |
if (![].map) { | |
avalon.mix(Array.prototype, { | |
//定位操作,返回数组中第一个等于给定参数的元素的索引值。 | |
indexOf: function(item, index) { | |
var n = this.length, | |
i = ~~index | |
if (i < 0) | |
i += n | |
for (; i < n; i++) | |
if (this[i] === item) | |
return i | |
return -1 | |
}, | |
//定位引操作,同上,不过是从后遍历。 | |
lastIndexOf: function(item, index) { | |
var n = this.length, | |
i = index == null ? n - 1 : index | |
if (i < 0) | |
i = Math.max(0, n + i) | |
for (; i >= 0; i--) | |
if (this[i] === item) | |
return i | |
return -1 | |
}, | |
//迭代操作,将数组的元素挨个儿传入一个函数中执行。Ptototype.js的对应名字为each。 | |
forEach: iterator('', '_', ''), | |
//迭代类 在数组中的每个项上运行一个函数,如果此函数的值为真,则此元素作为新数组的元素收集起来,并返回新数组 | |
filter: iterator('r=[],j=0,', 'if(_)r[j++]=this[i]', 'return r'), | |
//收集操作,将数组的元素挨个儿传入一个函数中执行,然后把它们的返回值组成一个新数组返回。Ptototype.js的对应名字为collect。 | |
map: iterator('r=[],', 'r[i]=_', 'return r'), | |
//只要数组中有一个元素满足条件(放进给定函数返回true),那么它就返回true。Ptototype.js的对应名字为any。 | |
some: iterator('', 'if(_)return true', 'return false'), | |
//只有数组中的元素都满足条件(放进给定函数返回true),它才返回true。Ptototype.js的对应名字为all。 | |
every: iterator('', 'if(!_)return false', 'return true') | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment