メッセージ
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 のターゲットではありません。この問題を回避するには、呼び出しを行うラムダを指定するか、this 引数に想定する型を強制するために Function.prototype.bind() 関数を使用するかのいずれかを行います。
例
不正な場合
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.