'use strict';
var fn = {};
// 拷贝collection
// 1 浅拷贝
// 2 可以处理"ArrayLike"
fn.toArray = function(collection) {
return [].slice.call(collection);
};
fn.cloneArray = fn.toArray;
fn.op = {
'+': function(value1, value2) {
return value1 + value2;
},
'-': function(value1, value2) {
return value1 - value2;
},
'*': function(value1, value2) {
return value1 * value2;
},
'/': function(value1, value2) {
return value1 / value2;
},
'==': function(value1, value2) {
return value1 == value2;
},
'===': function(value1, value2) {
return value1 === value2;
}
};
// 返回其类型
// null -> 'null'
// undefined -> 'undefined'
// slice(8,-1) 是因为 "[object String]"要截取"String"
fn.type = function(value) {
// If the value is null or undefined, return the stringified name,
// otherwise get the [[Class]] and compare to the relevant part of the value
return value == null ?
'' + value :
({}).toString.call(value).slice(8, -1).toLowerCase();
};
// 略
fn.is = function(value, type) {
return type === fn.type(value);
};
// args必需得是array
fn.apply = function(handler, args) {
return handler.apply(null, args);
};
// [[1,2],[3],[4,5] -> [1,2,3,4,5]
fn.concat = function() {
var args = fn.toArray(arguments);
return args[0].concat.apply(args[0], args.slice(1));
};
fn.js的源码欣赏(1)
最新推荐文章于 2024-09-11 05:28:45 发布