JS代码阅读

//通过predicate函数判断过滤数组
function filterArray(/*array*/a, /*boolean function*/ predicate) {
var results = [];
var length = a.length; // In case predicate changes the length!
for(var i = 0; i < length; i++) {
var element = a[i];
if (predicate(element)) results.push(element);
}
return results;
}

//Retrun the array of values that reaults when each of the elements
//Of the array a are passed to the function f
//返回a数组的所有值经过f函数运算后的结果数组
function mapArray(/*array*/a, /*function*/ f) {
var r = []; // to hold the results
var length = a.length; // In case f changes the length!
for(var i = 0; i < length; i++) r[i] = f(a[i]);
return r;
}



//给o对象绑定函数,并调用
function bindMethod(/*object*/o ,/*function*/ f) {
return function() { return f.apply(o, arguments) }
}

//返回一个函数调用函数f,带有指定参数和任何额外的参数传递给函数
function bindArguments(/*function*/f /*, initial arguments...*/) {
var boundArgs = arguments;
return function() {
//Build up an array of arguments. It start with the previously
//bound arguments and is extended with the arguments passed new
var args = [];
for(var i= 1; i < boundArgs.length; i++) args.push(boundArgs[i]);
for(var i= 0; i < arguments.length; i++) args.push(arguments[i]);

//Now invoke the function with these arguments
return f.apply(this,args)
}
}




//return an array that holds the names of the enumerable property of obj
//返回一个数组保存姓名的枚举属性对象
function getPropertyNames(/*object*/obj) {
var r = [];
for(name in obj) r.push(name);
return r;
}

//Copy the enumerable properties of the object from to the object to.
//if to in null, a new object is created. the function returns to or the
//newly created object.
//复制的可枚举属性的对象从对象。如果无效,一个新的对象被创建。该函数返回或者新创建的对象。
function copyProperties(/*object*/ from, /*optional object*/ to) {
if(!to) to = {};
for(p in from) to[p] = from[p];
return to;
}

//Copy the enumerable properties of the object from to the object to,
//but only the ones that are not already defined by to.
//This is useful, for example, when from contains default values that
//we want to use if they are not already defined in to.
//复制的可枚举属性的对象从对象,但只有那些没有已定义的。这是有用的,例如,当从包含默认值我们要使用如果他们没有已定义在。
function copyUndefinedProperties(/*objecr*/ from, /*objecr*/ to) {
for(p in from) {
if(!p in to) to[p] = from[p];
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值