Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not use this instead of null in method(lambda)? #74

Closed
adam-beck opened this issue Feb 27, 2014 · 1 comment
Closed

Why not use this instead of null in method(lambda)? #74

adam-beck opened this issue Feb 27, 2014 · 1 comment

Comments

@adam-beck
Copy link

In your method util,

/**
* Takes `lambda` function and returns a method. When returned method is
* invoked, it calls the wrapped `lambda` and passes `this` as a first argument
* and given arguments as the rest.
*
* @params {Function} lambda
* @returns {Function}
*/
function method (lambda) {
  return function () {
    return lambda.apply(null, [this].concat(Array.prototype.slice.call(arguments, 0)));
  };
}
exports.method = method;

you pass null as the first argument to apply. Why would you not pass this as the first argument so that you would have the ability to use this in the wrapped functions and have it point to the Poet instance. I'm aware you are making sure that the poet instance is always the first argument in the wrapped functions (so this would be redundant) but would it hurt any? Wouldn't having this point to GLOBAL be risky?

I am more trying to understand why than anything else.

@jsantell
Copy link
Owner

Just a functional helper I've picked up -- it does exactly what you said it does, just moving this to the first argument. It's only used internally for attaching methods on poet, leaving for more transparent unit testing (although I'm not sure if these functions are tested directly like that)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants