1

Could anyone explain what this function does. I dont understand the part where function definition parse(group) is being passed 'data'.

function getData(data) {
        data = (data === undefined) ? this.defaultData() : data;

        var self = this;

        return (function parse(group) {
            return self.change('groupTo', groupExp, group);
        }(data));
    }

Now that I know this is an IIFE, what I wanna know now is whether 'arg' is available for use within doSomething()

(function (local_arg) {
   doSomething(local_arg);
})(arg);
Luan Nico
  • 5,376
  • 2
  • 30
  • 60
Abhijit
  • 13
  • 3
  • 1
    If not a dup, but [good reading](https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript). – Teemu Aug 03 '17 at 07:26
  • I suppose you simplified the code, because as represented here, it is overly complicated for something that could be very simple. Could you maybe post the (more) original code? – trincot Aug 03 '17 at 07:39
  • Read the answer to the edited question at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Function_scope). – Teemu Aug 03 '17 at 07:58

3 Answers3

3

this is not a good piece of code. I would not know where to start on the bad practices...

This function defines a function that it is not reachable from any other scope, and not reusable, just to return its call with the data argument. The outer return could be simple as

return self.change('groupTo', groupExp, data);
JohnPan
  • 1,185
  • 11
  • 21
1

if you call getData() function without passing any parameter then value of the data variable in function is undefined. So at this Line ternary operator is used.

data = (data === undefined) ? this.defaultData() : data;

So it will check whether data === undefined condition which is true. therefore it will assign value of this.defaultData() to the data attribute In short when value of data is undefined that time following is the case

data = this.defaultData() 

Otheriwse if data has a value means calling function getData("Hi") with parameter then it will be evaluated as a

data = data // data = Hi

Now here var self = this; is used to preserve the context of this inside nested function which is mentioned below.

    return (function parse(group) {
        return self.change('groupTo', groupExp, group);
    }(data));

Without self = this if i try to use this in Nested function then it will point to the Global Object i.e window Object in JS.

In following Code arg is available inside the function as we are passing it in call of IIFE so it is availabel to pass in the call of doSomething function.

(function (local_arg) {
   doSomething(local_arg);
})(arg);
  • To Understand the How `this` behaves in JavaScript Read this Article http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/ – Chinmay Dalvi Aug 03 '17 at 07:46
  • Taken OP's code as it is, `this` will refer to `window` anyway. – Teemu Aug 03 '17 at 07:51
  • Yeah Currently function is Outside so it will points to window . But If this function is written in JavaScript Object then context will be change @Teemu – Chinmay Dalvi Aug 03 '17 at 07:53
  • It's a function declaration, it is not written in an object, it can be called in the context of an object, though. `this` also will get meaning, if the function is called with `new` operator. – Teemu Aug 03 '17 at 07:55
  • I was saying suppose if function is assign to the Object. obj1 ={ gt : function(data) { data = (data === undefined) ? this.defaultData() : data; var self = this; return (function parse(group) { return self.change('groupTo', groupExp, group); }(data)); } } Then context of `this` changes and this will points to the current JS object which is `obj1` rather than `window` object – Chinmay Dalvi Aug 03 '17 at 08:01
  • "Assigned" to an object is not the same as "written in an object" = ). Anywy, it is not assigned to or written in an object, it is a declared function ... As I said, It can be assigned to an object, or used as a constructor. – Teemu Aug 03 '17 at 08:02
1

In this pattern:

(function (local_arg) {
   doSomething(local_arg);
})(arg);

...the function is immediately executed, and the parameter local_arg will take the value of the argument that was passed, i.e. arg. So the above is doing the same as just:

doSomething(arg);

In some cases where arg is a more complicated expression, and you need to use it multiple times, or you have the need for variables that only need to be known locally, the IIFE pattern can be useful.

trincot
  • 317,000
  • 35
  • 244
  • 286