简体   繁体   中英

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; 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 .

Additionally, in a stack trace, you'd see render as the function name, rather than 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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