Learning JavaScript with MDN (call, apply, bind)
call, apply, bind
Object.prototype.toString()
检测 js 数据类型
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
Object.prototype.toString({})
// "[object Object]"
Object.prototype.toString([])
// "[object Object]"
Object.prototype.toString(Symbol())
// "[object Object]"
Object.prototype.toString(BigInt(1n))
// "[object Object]"
call, apply, bind
相同点: call, apply, bind 都是改变 this 的指向!
不同点:
- call 会立即执行, 参数列表(arg1, arg2, arg3, ...)
Object.prototype.toString.call([]);
// "[object Array]"
Object.prototype.toString.call(Symbol());
// "[object Symbol]"
Object.prototype.toString.call(BigInt(1n));
// "[object BigInt]"
- apply 会立即执行,参数数组 ([arg1, arg2, arg3, ...])
Object.prototype.toString.apply([]);
// "[object Array]"
Object.prototype.toString.apply(Symbol());
// "[object Symbol]"
Object.prototype.toString.apply(BigInt(1n));
// "[object BigInt]"
- bind 不会立即执行, 需要手动调用
Object.prototype.toString.bind([]);
// ƒ toString() { [native code] }
Object.prototype.toString.bind([])();
"[object Array]"
// "[object Array]"
Object.prototype.toString.bind(Symbol());
// ƒ toString() { [native code] }
Object.prototype.toString.bind(Symbol())();
// "[object Symbol]"
Object.prototype.toString.bind(BigInt(1n));
// ƒ toString() { [native code] }
Object.prototype.toString.bind(BigInt(1n))();
// "[object BigInt]"
refs
https://www.cnblogs.com/xgqfrms/p/9209381.html
https://www.cnblogs.com/xgqfrms/p/13019790.html
https://stackoverflow.com/questions/15455009/javascript-call-apply-vs-bind
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!