-3
var f1 = function(input) {
    var result = 1;
    return result;
}

var get_encrypted = function(func) {
    var str = 'zzoon';

    return function() {
        return func.call(null, str);
    }
}

var encrypted_value = get_encrypted(f1)();

I can't understand this code. what does func.call(null,str); mean? please explain this code , on the whole.

user2864740
  • 60,010
  • 15
  • 145
  • 220
YankeeCki
  • 41
  • 4
  • 1
    It means, call the function `fun` i.e. `f1` and pass it the param `str`. Can simply written as `func(str);` – Tushar Jan 24 '16 at 07:59
  • 3
    See [Function.prototype.call](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call). Whenever there is a question about a method, look first at the documentation. – user2864740 Jan 24 '16 at 08:03
  • The other parts of the question require "learning closures". See http://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1 and [Eloquent JavaScript: Higher-order Functions](http://eloquentjavascript.net/05_higher_order.html) as a start. – user2864740 Jan 24 '16 at 08:05
  • 1
    Note to close voters: You may find this question to be not useful, since the OP did not do any research. That's a fine reason to downvote. But I'm confused by voting to close. This question is certainly neither "too broad", nor certainly a "request for outside resource". Please vote to close only when a question is really off-topic, not just because you don't like it. That's what down votes are for. –  Jan 24 '16 at 09:30

1 Answers1

0

First, here's a reference to Function.prototype.call().

Basically, the first parameter in call() is the this object you desire to use in the called function. The called function in this case is f1, by the code: get_encrypted(f1).

So get_encrypted gets the function f1 as a parameter, and then calls f1, with this as null (when null is passed as first parameter). Now f1 can use this in its code, but in this case it would not be worthwhile as this would be null. f1 also get 'zzoon' as second parameter for input, but it ignores input.

When get_encrypted gets called, by the code get_encrypted(f1)();, it returns the result (of a function that returns the result) of the function f1, where f1 used or might have used null as this. This result is 1.

guysigner
  • 2,822
  • 1
  • 19
  • 23