//判断数据类型
Type = {};
for(var i=0,type;type=['String','Array','Number'][i++];){
(function(type){
Type['is'+type] = function(obj){
return Object.prototype.toString.call(obj) === '[object '+type+']';
}
})(type)
}
console.log('type',Type.isArray([]));
//动态植入方法
Function.prototype.before = function(beforefn){
var _self = this;
return function(){
beforefn.apply(this,arguments);
return _self.apply(this.arguments);
}
}
Function.prototype.after = function(afterfn){
var _self = this;
return function(){
var ret = _self.apply(this.arguments);
afterfn.apply(this,arguments);
return ret;
}
}
var func = function(){
console.log(2)
}
func = func.before(function(){
console.log(1);
}).after(function(){
console.log(3)
})
func();
//currying函数柯里化
var coast = (function(){
var args = [];
return function(){
if(arguments.length === 0){
var a = 0;
for(var i=0;i<args.length;i++){
a+=args[i]
}
return a;
}else{
[].push.apply(args,arguments);
}
}
})()
coast(100);
coast(200);
console.log('***',coast());
//call,apply
//1检测对象类型
console.log(Object.prototype.toString.call('123') === '[object String]')
//2类数组转换数组 取第一个参数
function test2(){
let arr = Array.prototype.slice.call(arguments);
let first = Array.prototype.shift.call(arguments);
console.log('first',first)
}
test2('a',2,3,4)
//3判断属性是否在对象上
let myObject = {a:'1'}
console.log('3',Object.prototype.hasOwnProperty.call(myObject,'aaa'));
//4两个数组合并
let iarr1 = [1,2,3];
let iarr2 = [4]
Array.prototype.push.apply(iarr1,iarr2)
console.log(iarr1)
//5得到数组最小(最大)的一项
console.log(Math.min.apply(null,[4,9,1,2,3]))