Array.prototype.map()

Syntax

var new_array = arr.map(callback[, thisArg])

Parameters

callback
Function that produces an element of the new Array, taking three arguments:
currentValue
The current element being processed in the array.
index
The index of the current element being processed in the array.
array
The array map was called upon.
thisArg
Optional. Value to use as this when executing callback.
Return value

A new array with each element being the result of the callback function.

Examples

var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]

Array.prototype.filter()

Syntax

var new_array = arr.filter(callback[, thisArg])

Parameters

callback
Function is a predicate, to test each element of the array. Return true to keep the element, false otherwise, taking three arguments:
element
The current element being processed in the array.
index
The index of the current element being processed in the array.
array
The array filter was called upon.
thisArg
Optional. Value to use as this when executing callback.

Examples

function isBigEnough(value) {
  return value >= 10;
}

var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]

ECMAScript 3 中call 和apply的应用

call()和apply()是某个对象上的默认方法,通过调用这两个方法的可以改变this的指向。
call()和apply()的第一个参数是将this重新定义为的对象,
举个栗子

function C1(){
	this.name='张三';
	this.age='24';
	this.sayname=function(){
		console.log("这里是C1类,我的名字是:"+this.name+"我的年龄是"+this.age);
	}
}
function C2(){
	this.name='李四';
	this.age='25';
}
var c1=new C1();
var c2=new C2();
c1.sayname();
c1.sayname.call(c2);

执行结果:
这里是C1类,我的名字是:张三我的年龄是24
这里是C1类,我的名字是:李四我的年龄是25

通过call()和apply()方法改变this指向实现继承

function Human(name,sex){
this.name=name;
this.sex=sex;
this.walk=function(){
console.log(‘我在走路’);
}
}
function Child(){
Human.call(this,”小明”,”男”)
this.paly=function(){
console.log(‘我很喜欢玩耍’);
}
this.intruduce=function(){
console.log(‘大家好,我是’+this.name);
}
}
var jinp=new Human(‘Jack’,’男’);
var xiaoping=new Child();
xiaoping.walk();
xiaoping.paly();
xiaoping.intruduce();

call方法和apply方法的却别在于入参不一样,apply接收的是数组
例如

Math.max.apply(null,array)求数组中最大值

ECMAScript 5中新增bind方法,bind方法和他们一样,也是Function的prototype中的方法作用是将函数的this指向对象
参数是个对象
举个栗子

function introduce(country,hobby){
return “大家好,我叫”+this.name+”, 今年”+this.age+”岁, 来自”+country+”, 喜欢”+hobby;
}
var xiaoming={name:”小明”,age:20}
var jieshao=introduce.bind(xiaoming);
console.log(jieshao(“中国”,”打球”));

执行结果:
大家好,我叫小明, 今年20岁, 来自中国, 喜欢打球

当然bind完全可以通过apply和call来模拟
例如这样其实效果是一样的

function introduce(country,hobby){
return “大家好,我叫”+this.name+”, 今年”+this.age+”岁, 来自”+country+”, 喜欢”+hobby;
}
var xiaoming={name:”小明”,age:20}
console.log(introduce.apply(xiaoming,[“中国”,”打球”]));
//或者下面这个
console.log(introduce.call(xiaoming,”中国”,”打球”));

参考文档

Array
javascript中call和apply函数的比较和应用