TypeError: Function.prototype.toString called on incompatible object (Firefox) TypeError: Function.prototype.bind called on incompatible target (Firefox) TypeError: Method Set.prototype.add called on incompatible receiver undefined (Chrome) TypeError: Bind must be called on a function (Chrome)é误类å åªéåºéäºï¼
å½è¿ä¸ªé误被æåºæ¶ï¼å±äºæä¸ªå¯¹è±¡ç彿°è¢«è°ç¨ï¼ä½æ¯æ²¡ææä¾ç» this
ä¸å
¶æææç彿°ç±»åä¸è´çåæ°ã
å¨è°ç¨ Function.prototype.call()
æFunction.prototype.apply()
æ¹æ³ï¼ä½æ¯ä¸º this
æä¾çç»å®å¯¹è±¡ä¸é¢æçä¸å¹é
æ¶ï¼è¿ä¸ªé®é¢å°±ä¼åºç°ã
该é®é¢è¿ä¼åºç°äºå°ä¸ä¸ªï¼åå¨å¨ä¸ä¸ªå¯¹è±¡ä¸çï¼å½æ°æä¾ç»å¦å¤ä¸ä¸ªå½æ°ä½ä¸ºåæ°æ¶ãå¨è¿ç§æ
åµä¸ï¼å½æ°æå¨ç对象并éè¯¥å½æ°ç this
ç»å®çç®æ 对象ã为äºè§£å³è¿ä¸ªé®é¢ï¼ä½ æè
为å
¶æä¾ä¸ä¸ª lambda 表达å¼ï¼è¯¥è¡¨è¾¾å¼å®æå½æ°è¦å®æçä»»å¡ï¼æè
è°ç¨ Function.prototype.bind()
彿°ä¸º this
强å¶ç»å®ææç对象ã
var mySet = new Set();
["bar", "baz"].forEach(mySet.add);
// mySet.add is a function, but "mySet" is not captured as this.
var myFun = function () {};
["bar", "baz"].forEach(myFun.bind);
// myFun.bind is a function, but "myFun" is not captured as this.
æ£ç¡®ç¤ºä¾
var mySet = new Set();
["bar", "baz"].forEach(mySet.add.bind(mySet));
// This works due to binding "mySet" as this.
var myFun = function () {};
["bar", "baz"].forEach((x) => myFun.bind(x));
// This works using the "bind" function. It creates a lambda forwarding the argument.
ç¸å
³å
容
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4