//代码块
1. <script>
var obj = {}
function foo(a, b) {
console.log('-----------------------------------------');
console.log(a);
console.log(b);
console.log(this);
if (typeof this === 'function') this()
console.log(this.constructor === String);
if (this.constructor === String) {
// 可以触发string中 api
var a = this.replace('1', '狗')
console.log(a);
}
}
// call()
// 作用: 触发函数,并改变function函数中this,this为参数一
// 参数二,三... 给被触发的函数传入实参的.
// window.foo(1,2)
// foo.call(obj, 3, 2)
// foo.call([], 4, 5)
// foo.call(function () {
// console.log('hahah');
// }, 2, 3)
// foo.call('123',3,4)
// apply
// 参数一,改变触发函数方法题中this this 为参数一
// 参数二:数组,给触发函数传递 n个实参
// foo.apply(obj,[2,3])
// foo.apply([],[2,3])
// foo.apply(123,[2,3])
// bind
// 作用:更改触发函数中的this指向.
// 函数不能自动执行,需要再加小括号
// 返回值为新的函数,函数与 foo 一样功能的函数
var bar = foo.bind();
bar();
var bar1 = foo;
// console.log(foo)
// console.log(bar);
foo.bind(obj)(2,3)
foo.bind([])(2,3)
foo.bind(123)(2,3)
// 问 bar 与 bar1 区别是什么?
// bar1 与 foo 是同一个函数
// bar 与foo 不是同一个函数,但是功能一摸一样
</script>