JavaScript参考手册
错误 | Errors
Errors: Called on incompatible type
信息
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)
错误类型
TypeError
哪里错了?
当这个错误被抛出时,属于某个对象的函数被调用,但是没有提供给 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.
错误 | Errors相关

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络