挥舞函数
JavaScript 标准库 – Function
原型对象 – 方法
Function.prototype.apply()
在一个对象的上下文中应用另一个对象的方法;参数能够以数组形式传入。
Function.prototype.call()
在一个对象的上下文中应用另一个对象的方法;参数能够以列表形式传入。
Function.prototype.bind()
bind()方法会创建一个新函数,称为绑定函数.当调用这个绑定函数时,绑定函数会以创建它时传入 bind()方法的第一个参数作为 this,传入 bind()方法的第二个以及以后的参数加上绑定函数运行时本身的参数按照顺序作为原函数的参数来调用原函数.
函数表达式
- 函数声明非常容易(经常是意外地)转换为函数表达式
- 函数表达式不会提升,所以不能在定义之前调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23// 函数声明
function foo() {}
// 函数表达式
(function bar() {})
// 函数表达式
x = function hello() {}
if (x) {
// 函数表达式
function world() {}
}
// 函数声明
function a() {
// 函数声明
function b() {}
if (0) {
//函数表达式
function c() {}
}
}
匿名函数
4.2.1
普通命名函数的中的递归
检测回文1
2
3
4
5function isPalindrome(text) {
if (text.length <= 1) return true;
if (text.charAt(0) != text.charAt(text.length - 1)) return false;
return isPalindrome(text.substr(1, text.length - 2));
}
其他实现1
2
3
4
5
6
7
8
9
10
11
12
13
14
15function isPalindrome(line) {
line += "";
return line === line.split("").reverse().join("");
}
// 这种方法很方便,但效率不高,字符串分割,倒转,聚合都需要很多额外的操作。
// 最好的方法,是从字符串头部和尾部,逐次向中间检测。
function isPalindrome(line) {
line += "";
for (var i = 0, j = line.length - 1; i < j; i++, j--) {
if (line.charAt(i) !== line.charAt(j)) {
return false;
}
}
return true;
}
4.3.2
自记忆函数
缓存记忆昂贵的计算结果1
2
3
4
5
6
7
8
9
10
11
12
13
14function isPrime(value) {
if (!isPrime.answers) isPrime.answers = {}; //#1
if (isPrime.answers[value] != null) { //#2
return isPrime.answers[value]; //#2
} //#2
var prime = value != 1; // 1 can never be prime
for (var i = 2; i < value; i++) {
if (value % i == 0) {
prime = false;
break;
}
}
return isPrime.answers[value] = prime; //#3
}
4.3.3
伪造数组方法
JavaScript 函数基础
定义方法
- 静态方法
function 函数名(){return[函数返回值]}
- 动态匿名方法
var 函数名 = new Function ([arg]){}
- 直接量方法
函数名 = new Function ([arg]){函数体}
调用方法
- 直接调用
- 在链接中调用
- 在事件中调用
- 递归调用
方法
Function.prototype.apply()
func.apply(thisArg, [argsArray])
返回值:调用有指定this值和参数的函数的结果
thisArg
可选。在非严格模式下,则指定为 null 或 undefined 时会自动替换为指向全局对象(浏览器中就是window对象),同时值为原始值(数字,字符串,布尔值)的 this 会指向该原始值的包装对象。
argsArray
可选。一个数组或者类数组对象,其中的数组元素将作为单独的参数传给 func 函数。如果该参数的值为null 或 undefined,则表示不需要传入任何参数。从ECMAScript 5 开始可以使用类数组对象。
使用apply来链接构造器1
2
3
4
5Function.prototype.construct = function (aArgs) {
var oNew = Object.create(this.prototype);
this.apply(oNew, aArgs);
return oNew;
};
Function.prototype.call()
fun.call(thisArg, arg1, arg2, ...)
返回值:返回值是你调用的方法的返回值,若该方法没有返回值,则返回undefined。
Function.prototype.bind()
fun.bind(thisArg[, arg1[, arg2[, …]]])
参数
thisArg
当绑定函数被调用时,该参数会作为原函数运行时的 this 指向。当使用new 操作符调用绑定函数时,该参数无效。
arg1, arg2, ...
当绑定函数被调用时,这些参数将置于实参之前传递给被绑定的方法。
返回值
返回由指定的this值和初始化参数改造的原函数拷贝
创建绑定函数
示例
1 | this.x = 9; |
偏函数
1 | function list() { |
arguments
- 功能:存放实参的参数列表
- 特性:1. 仅能在函数体内使用
2. 带有下标属性,非数组 3. 函数声明时自动初始化
- 属性:1. length
2. callee(严格模式下,(ES5) 禁止使用) 3. caller 已被移除
箭头函数
箭头函数有几个使用注意点
函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。
不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。
不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用 rest 参数代替。
不可以使用yield命令,因此箭头函数不能用作 Generator 函数。
上面四点中,第一点尤其值得注意。this对象的指向是可变的,但是在箭头函数中,它是固定的。