3

i was trying to use the javascript APPLY function to pass scope from 1 function to another, but it seems that i might be doing it wrong?

Here is a fiddle if you need it: http://jsfiddle.net/C8APz/

Here is my code:

function a(){
    var x = "hello";
    b.apply(this, []);
}

 function b(){
    console.log("x: ", x);
}

a();

i was thinking that while the scope is passed, the variables / variable reference are not.

Is there a way to do something like this without defining Globals?

Should i add the data to the actual part of it, such as this.x = x;? and then in the other function just fetch it? var x = this.x;

function a(){
    var x = "hello";
    this.x = x;
    b.apply(this, []);
}

 function b(){
    var x = this.x;
    console.log("x: ", x);
}

a();

Edit: It seems that the second example assigns in the global scope, which isnt good, and with the scope, i was attempting to pass an understanding of context to. It seems that you really have to define a context before you pass it, otherwise, for the most part this refers to window

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • That isn't what `apply` does. You cannot pass a scope from one function to another. – user229044 May 27 '14 at 12:39
  • The scope cannot be passed like this, however the scope can flow if you define b within a, see [fiddle](http://jsfiddle.net/C8APz/1/). – scragar May 27 '14 at 12:39
  • the second code segment does in fact work, so i assumed that you can infact pass scopes if things are properly defined inside of `this` though im not sure if it is good form to do so. I usually use `$.apply` when extending functions in OOP design – Fallenreaper May 27 '14 at 12:42
  • @Fallenreaper — Don't confuse scope and context. – Quentin May 27 '14 at 12:44
  • ooh! Yea, that mightve been my issue. :) I was thinking scope was the lifecycle of a function and all the variables defined therein. Thats why I was thinking i could write an extension in which would do this. – Fallenreaper May 27 '14 at 12:46
  • @Quentin I guess, i should write up a new question about passing context around, or will i end up arriving at the same answer? – Fallenreaper May 27 '14 at 12:47

2 Answers2

3

You cannot pass scope around.

You can either move the function declaration for b inside the function declaration for a so that the scope is right to start with, or you can pass the variables you care about using arguments.


function a(){
    var x = "hello";
    b();

    function b(){
        console.log("x: ", x);
    }
}

a();

function a(){
    var x = "hello";
    b(x);
}

function b(x){
    console.log("x: ", x);
}

a();
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I did notice that i can do something like my second code sample. Is that bad form? – Fallenreaper May 27 '14 at 12:41
  • In strict mode, you are trying to set a property on `undefined` which will throw an exception. In non-strict mode, you are setting a property on the default object (`window` in a browser), which is a global and thus has all the usual warnings about globals associated with it. – Quentin May 27 '14 at 12:43
  • I just noticed the edit. Yea, i didnt want to change the passed in args as those are for other variables. I was trying to create a custom flag which is in a rare case, in which i dont add additional parameters. – Fallenreaper May 27 '14 at 12:43
  • oh. Ok. Yea, i was thinking that given the scope of the function lasts until it finished executing, that i could extend the function with my intended goals. Maybe im thinking moreso of different languages. – Fallenreaper May 27 '14 at 12:44
0

Declare var x; out of the function body...

var x;
function a(){
    x = "hello";
    b.apply(this, []);
}

function b(){
    //a.apply(this,[]);
    console.log("x: ", x);
}

a();
Devansh
  • 1,277
  • 1
  • 13
  • 19
  • Question had: **Is there a way to do something like this without defining Globals?** – Fallenreaper May 27 '14 at 12:58
  • yea, i wasnt trying to do it that way. it is why i am trying to pass scope/context around such that variables would be assigned without changing the underlying code segments. That way: x is already assigned, and in the child function, i could just say things like: `if(typeof x != 'undefined') console.log("x:", x);` – Fallenreaper May 27 '14 at 13:03
  • http://stackoverflow.com/questions/407048/accessing-variables-from-other-functions-without-using-global-variables – Devansh May 27 '14 at 13:07
  • Yea, my second attempt was trying to make it a property of this, though it just essentially made it a global in the process. >_> – Fallenreaper May 27 '14 at 13:14