www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Accessing things in a self executing function

Hybrid View

  1. #1
    Join Date
    Jan 2014
    Posts
    28

    Accessing things in a self executing function

    So If I have a self-executing function and I want to access a variable in another self executing function is there anyway to do this?

    If their is a way, Is their a way without it being a reference to the original?

    Code:
    (function () {
        
        var accessMe = "Hello World"; // What I want to grab
    
    })();
    
    
    (function () {
    
        accessMe = "Goodbye World"; // What I want to change to
    
    })();

  2. #2
    Join Date
    Feb 2014
    Location
    Managua, Nicaragua
    Posts
    38
    The have to use a global variable, declare the variable outside of function first:

    Code:
    var accessMe; //global variable
    Then use it, chance the value or whatever u want inside the function...

    Code:
    (function () {
        accessMe = "Hello World"; // What I want to grab
    })();
    
    
    (function () {
        accessMe = "Goodbye World"; // What I want to change to
    })();
    when u use the term 'var', u are setting the var local to the function, so, take off the term 'var' from the first 'accessMe' and then try...

    hope to be hopeful...

  3. #3
    Join Date
    Jan 2014
    Posts
    28
    Quote Originally Posted by JCPerez View Post
    The have to use a global variable, declare the variable outside of function first:

    Code:
    var accessMe; //global variable
    Then use it, chance the value or whatever u want inside the function...

    Code:
    (function () {
        accessMe = "Hello World"; // What I want to grab
    })();
    
    
    (function () {
        accessMe = "Goodbye World"; // What I want to change to
    })();
    when u use the term 'var', u are setting the var local to the function, so, take off the term 'var' from the first 'accessMe' and then try...

    hope to be hopeful...
    So not setting var in front of a variable makes it a global variable even if it's in a function?

  4. #4
    Join Date
    Feb 2014
    Location
    Managua, Nicaragua
    Posts
    38
    here is a simple test...

    Code:
    <script>
    //setting 'hola' global
    var hola = '12345';
    
    //new instance of hola
    function first(){
    	var hola = '67890';
    	return hola;
    }
    
    //taking the global variable
    function second(){
    	return 'numbers: ' + hola;
    }
    
    document.write(hola + '<br/>');
    document.write(first() + '<br/>');
    document.write(second() + '<br/>');
    </script>

  5. #5
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    247
    Code:
    console.log((function() {
      return (function() {var msg ="Hello world!!"; return msg}())
    }()))
    Or you can return an anonymous function and then call it:

    Code:
    console.log((function() {
      return (function() {return "Hello world!!"});
    })()())
    no need for global variables.


    If their is a way, Is their a way without it being a reference to the original?
    Not sure what you have meant by this. When a function finishes its execution its data is destroyed if there is no any reference to it anymore.
    Last edited by tech_soul8; 02-14-2014 at 12:50 PM.

  6. #6
    Join Date
    Feb 2014
    Location
    Managua, Nicaragua
    Posts
    38
    if u no set the term 'var', the function wil take the global variable, but if u put the term 'var', it will be create a new var localy to the function...

    sorry if i am not clear, but i dont speak english, but i am trying as well xD

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,352
    This expands on 'JCPerez's example...
    Code:
    <script>
    //setting 'hola' global
    var hola = '12345';
    
    // 'hi' is also global without the var
    hi = 'hello';
    
    //new instance of hola
    function first(){
    	var hola = '67890';
    	return hola;
    }
    
    //taking the global variable
    function second(){
    	return 'numbers: ' + hola;
    }
    
    function third(){
      bye = 'goodby';  // NOTE:  This is also global without the var
      var adios = "so long, it's been good to know you";
    	return 'letters: ' + hi + ' - ' + bye + ' - ' + adios;
    }
    
    document.write(hola + '<br/>');
    document.write(first() + '<br/>');
    document.write(second() + '<p/>');
    document.write(hi + '<br/>');
    document.write(third() + '<br/>');  // set inside function but defaults to global without var
    document.write(bye + '<br/>');      // still global
    document.write('Finally: ' + adios + '<br/>');    // undefined outside of function -- see error console
    document.write('Done with examples');             // not show due to error on line above.
    </script>
    Last edited by JMRKER; 02-14-2014 at 02:37 PM.

  8. #8
    Join Date
    Feb 2014
    Location
    Managua, Nicaragua
    Posts
    38
    Excelent @JMRKER

  9. #9
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    247
    Although not mandatory it is very important to declare each of your variables with the var keyword.

    One situation nobody haven't mentioned here is when you have a global variable and want to set local variable with the same name. Consider this example:

    Code:
    var msg = 'global';
    
    function printVar() {
    msg = 'local' // note that var keyword is missing
    return msg;
    }
    
    console.log(printVar())
    console.log(msg)
    
    //using the var keyword
    function printVar() {
    var msg='local'
    return msg
    }
    
    console.log(printVar())
    console.log(msg)

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,352

    Question

    Quote Originally Posted by tech_soul8 View Post
    Although not mandatory it is very important to declare each of your variables with the var keyword.

    One situation nobody haven't mentioned here is when you have a global variable and want to set local variable with the same name. Consider this example:

    Code:
    var msg = 'global';
    
    function printVar() {
    msg = 'local' // note that var keyword is missing
    return msg;
    }
    
    console.log(printVar())
    console.log(msg)
    
    //using the var keyword
    function printVar() {
    var msg='local'
    return msg
    }
    
    console.log(printVar())
    console.log(msg)
    I'm not entirely clear what you are trying to demonstrate with the code
    when you have two different functions with the same name.

    Not a normal way to code.
    Can you elaborate?

  11. #11
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    247
    Quote Originally Posted by JMRKER View Post
    I'm not entirely clear what you are trying to demonstrate with the code
    when you have two different functions with the same name.

    Not a normal way to code.
    Can you elaborate?
    Noo, I just wanted to spare myself from some extra typing

    What I tried to explain is if you have a global variable and you declare a function within you want to use that same name for a local variable if you not use the var keyword you're going to effectively overwrite the valu of a glob variable and that's something you probably didn't want to do.

    Example:

    Code:
    var msg = 'global'
    
    function printMsg() {
    msg = 'local'
    return msg
    }
    
    console.log(printMsg())  //prints local
    console.log(msg) //prints local
    Code:
     
    var msg = 'global'
    
    function printMsg() {
    var msg = 'local'
    return msg
    }
    
    console.log(printMsg()) //prints local
    console.log(msg) //prints global
    Last edited by tech_soul8; 02-14-2014 at 07:06 PM.

  12. #12
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    247
    ...although maybe not so important but thing to know is that variables once declared with the var keyword cannot be deleted with the delete operator.

  13. #13
    Join Date
    Jan 2014
    Posts
    28
    Awesome thanks a lot guys. And thanks again tech, Always quick on the responses. Almost finished with my second class now. I generally understand scope but theirs a few odds and ends that haven't been defined yet, Like this for example.

  14. #14
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    247
    No problem man
    And thanks again tech, Always quick on the responses
    Sometimes I have a lot of 'free' time at work so I'm bored

    There's also one interesting feature and it's called hoisting.

    Example:

    Code:
    var scope = 'global'
    
    function checkScope() {
    console.log(scope)
    
    var scope = 'local'
    console.log(scope)
    }
    
    checkScope()
    Can you explain this?
    Last edited by tech_soul8; 02-14-2014 at 09:31 PM.

  15. #15
    Join Date
    Jan 2014
    Posts
    28
    Quote Originally Posted by tech_soul8 View Post
    No problem man


    Sometimes I have a lot of 'free' time at work so I'm bored

    There's also one interesting feature and it's called hoisting.

    Example:

    Code:
    var scope = 'global'
    
    function checkScope() {
    console.log(scope)
    
    var scope = 'local'
    console.log(scope)
    }
    
    checkScope()
    Can you explain this?
    Yeah I believe I understand that one, The first console.log is undefined because the variable is not global but the second console.log says "local" because your logging it inside of the function where the variable exists right?

    And I'll have to look up JSON vars \\.\, I have yet to learn JSON I'm only in javascript fundamentals.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles