【发布时间】:2016-05-26 21:00:51
【问题描述】:
我已经阅读了很多关于 JavaScript 中的 call() 和 bind() 的内容,特别是关于 MDN article 的创建快捷方式部分。
我正在尝试在 JS 中实现以下 Python 风格的函数:
var arr = [1,2,3];
len(arr); // 3
我确实意识到这是一个人为的例子,但我正试图围绕这些方法展开我的头脑。以下是我的实现方式:
var len = Function.prototype.call.bind( Array.prototype.slice.length );
len([1,2,3]);
当我运行它时,我得到:
len([1,2,344])
^
TypeError: len is not a function
at Object.<anonymous> (/private/var/folders/j6/3fs5_k3n17z_0j2xrwj6sphw0000gn/T/CodeRunner/Untitled 11.js:2:1)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3
我在这里缺少什么来了解它是如何工作的?
【问题讨论】:
标签: javascript python call bind apply