JavaScript ã®ä¾å¤ "called on incompatible target (or object)" ã¯ã颿°ã (ä¸ãããããªãã¸ã§ã¯ãä¸ã§) å¼ã³åºãããã¨ãã«ã颿°ãæå¾
ããåã«å¯¾å¿ãã¦ããªã this
ã使ç¨ãã¦å¼ã³åºãããå ´åã«çºçãã¾ãã
TypeError: 'this' is not a Set object (Edge)
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
ã®ã¿ã¼ã²ããã«ã¯ãªãã¾ããããã®åé¡ãåé¿ããã«ã¯ãå¼ã³åºããè¡ã£ã¦ããã©ã ããæä¾ãããã 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 () {
console.log(this);
};
["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 () {
console.log(this);
};
["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