简体   繁体   English

命名对象属性函数

[英]Named Object Property Functions

render: function render(context, partials) {
  return this.r(context, partials);
},

Given this code from Twitter's new hogan.js library to demonstrate the issue; 从Twitter的新hogan.js库中获取此代码来演示该问题; what is the goal of naming the function twice? 命名函数两次的目标是什么?

If it wanted to, the function render would be able to call itself via render() , however, render() is not accessible anywhere else . 如果需要,函数render将能够通过render()调用自身 ,但是render() 在其他任何地方都不可访问。

Additionally, in a stack trace, you'd see render as the function name, rather than anonymous function . 此外,在堆栈跟踪中,您将看到render作为函数名称,而不是anonymous function

它是递归调用所必需的。

The first occurance of render is the name of the field the function is stored in, so that you can call the function via 渲染的第一个出现是函数存储的字段的名称,以便您可以通过调用函数

object.render(context, partials);

The second render names the function itself. 第二个渲染器命名函数本身。 Debugging tools then display render instead of only function . 然后调试工具显示渲染而不是仅显示功能

A second, possible reason is that the function could call itself recursively. 第二个可能的原因是函数可以递归调用自身。

var render = function render(n) {
    console.log("render");
    if (n < 1)
      render(n + 1);
};
render(0);

Edit: Sorry, I've written something really wrong in the first revision. 编辑:对不起,我在第一次修订中写错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM