0%

hammerspoon

http://www.hammerspoon.org/

https://github.com/greyby/hammerspoon

apply 和 call 的区别

1
2
3
4
5
6
7
8
9
var obj = {
name : 'linxin'
}

function func(firstName, lastName){
console.log(firstName + ' ' + this.name + ' ' + lastName);
}

func.apply(obj, ['A', 'B']); // A linxin B

call 方法第一个参数也是作为函数上下文的对象,但是后面传入的是一个参数列表,而不是单个数组。

1
2
3
4
5
6
7
8
9
var obj = {
name: 'linxin'
}

function func(firstName, lastName) {
console.log(firstName + ' ' + this.name + ' ' + lastName);
}

func.call(obj, 'C', 'D'); // C linxin D

bind

1
2
3
4
5
6
7
8
9
function func(a, b, c) {
console.log(a, b, c);
}
var func1 = func.bind(null,'linxin');

func('A', 'B', 'C'); // A B C
func1('A', 'B', 'C'); // linxin A B
func1('B', 'C'); // linxin B C
func.call(null, 'linxin'); // linxin undefined undefined

via https://github.com/lin-xin/blog/issues/7