SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How do I make function a variable

    I want to make the whole function a variable and use it in other functions like something below:
    Code:
    var a = (function() {
    	var x = '5';
    	b(x);
    })();
    
    function b(i) {
    	this.i = i;
            // console.log(i); // 5
    	return i;
    }
    
    var c = function() {
    	console.log(b);
    }
    
    var button = document.getElementById('button');
    button.addEventListener("click", c, false);
    How do I display b as 5 instead of function code.
    Thank you,

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    You can't. In JavaScript you execute a function with brackets()

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks for response. Is there any other way around I can achieve this?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,836
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    I can't understand your question.

    Functions in JavaScript can be passed around just like any other variable - just leave the () off the end as that runs the function rather than just referencing it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sorry for my bad English. In a real practice, I extract a variable from function a. It's a multiple-nested function. So, I store the value of the variable in a function to be further use it. Now that I want to use that value in another function.

    I can't extract that value from that function and try to figure it out.

    Hope that's clear. Thanks

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,836
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    For passing variables between functions you have three choices.

    1. Define the variable outside of both functions.
    2. Return the variable from the first function so you can pass it to the second
    3. Make both functions methods of the same object and define the variable as a property of that object.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank you,

    I make it a global variable. So it can be access from anywhere. I know it's bad practice but I can get my job done.


Bookmarks

Posting Permissions

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