頹廢就在那一瞬間,那段時(shí)間。
bind , call && apply 通俗的來講就是窘问,改變作用環(huán)境(this指向)烛芬。
this指向:最后調(diào)用它的對象隧期,(匿名函數(shù)this指向window)
-
bind()
bind()
與call,apply區(qū)別:綁定之后然后--調(diào)用(不會(huì)立即執(zhí)行)。
const obj = {
a: '1',
b: '2'
}
function fun1(val) {
console.log(this);
}
fun1.bind(obj)() //打印 {a:'1',b:'2'}
//如果不bind將會(huì)打印window
-
call && apply
call && apply
與bind區(qū)別:綁定之后立即執(zhí)行
call與apply區(qū)別:call是apply的語法糖赘娄,call參數(shù)為單個(gè)參數(shù)仆潮,apply參數(shù)為數(shù)組
/*
call
*/
const obj = {
a: '1',
b: '2'
}
function fun1(val) {
console.log(this, val);
}
fun1.call(obj, ['a', 'b', 'c'])
//打印{a: "1", b: "2"} , ["a", "b", "c"]
/*
apply
*/
const obj = {
a: '1',
b: '2'
}
function fun1(val) {
console.log(this, val);
}
fun1.call(obj, ['a', 'b', 'c'])
//打印{a: "1", b: "2"} 遣臼, "a"
- 拓展關(guān)于new的過程
//構(gòu)造函數(shù)
function fun(){...}
new fun{
let obj={}
obj.__proto__=fun.prototype
let result=fun.call(obj,...)
return typeof result === 'obj'?result:obj
}