xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Learning JavaScript with MDN (call, apply, bind) All In One

Learning JavaScript with MDN (call, apply, bind) All In One

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 的指向!

不同点:

  1. 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]"

  1. 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]"

  1. 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]"

demos

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

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-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-07-31 18:14  xgqfrms  阅读(314)  评论(3编辑  收藏  举报