JavaScript must know the usage instructions of of 10 call apply bind
- 2021-06-28 08:23:10
- OfStack
call
Each func inherits methods such as call apply.
function print(mesage)
{
console.log(mesage);
return mesage;
}
print.call(this, "cnblogs");//cnblogs
call (thisAgr, agr1, agr2...), call method first passes an context context.Following is the number of parameters.
apply
apply (thisAgr, [agr1, agr2]), apply method and call usage 1, but the parameters passed will be different.
function print(a,b)
{
console.log(a + b);
}
print.apply(this, ["hello","cnblogs"]);
bind
bind binds a function to touch an object.
<script>
function f(y) { alert(this.x + y); }
var o = { x: };
var g= f.bind(o);
g();//
</script>
summary
The call to the call apply function is the same as the call to the function directly.
call apply can also mimic the bind method.
<script>
function f(y) { alert(this.x + y); }
var o = { x: };
f.call(o, );//
f.apply(o, []);//
</script>