1

In a curry function like this :

var curry = function() {
    var slice = Array.prototype.slice,
        args = slice.call(arguments),
        fn = args.shift();

    return function(){
        return fn.apply(null, args.concat(slice.call(arguments)));
    };
};

is there any difference between this or null in fn.apply ? I don't see a case where it could make a difference.


EDIT :

Thanks to this answer I think it's pretty clear now, here is a little example I made to undersand it :

function msg() {
    console.log(this.name);
}

var foo = { name: "foo"};

var msg_this = curry_this(msg);
var msg_null = curry_null(msg);

msg_this();         //msg.call(null) -> undefined
msg_null();         //msg.call(null) -> undefined
msg_this.call(foo); //msg.call(foo) -> foo
msg_null.call(foo); //msg.call(null) -> undefined

with curry_this returning fn.apply(this,... and curry_null returning fn.apply(null...

4

1 Answer 1

1

Passing null to apply makes the context the global one (window in a browser).

From the MDN :

if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed.

How this will impact the result depends on the fn function (the first argument to curry) and on how you call it.

See this code :

var a = {curried: curry(function(){console.log(this)})};
a.curried();

If you pass null to apply, it logs window instead of the object a.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.