Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Exploring closures in functions in depth

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 622
    Answer it
    var ClosureDem = function (arg1) {
      return function (arg2) {
        return arg1 + arg2;
        }
    }
    ClosureDem("Hello")(" world!");//-> "Hello world!"

    Recently, I started learning about closures in functions and found a lot of new things for myself but still there are some things to be clarified. For example, while, the example above is quite understandable, I wonder is it possible to add new sets of arguments programmatically? What I mean is, I would like to do something like this,

    ClosureDem("I")("am")("a")("coder")//-> "I am a coder"

    The number of args should not matter. I am a newbie, so any help will be appreciated!

 1 Answer(s)

  • Hello entusias,

    I just checked your code and i want to recommend you to pass one extra argument in first function which we contain the integer value to create other functions with this count. Suppose you created functions like below.

    var myshort = function (arg1,count)
    {
    var arg = arg1;
    for(var i=0;i<count;i++){
       return function(arg + i){
        return $arg += $arg + count; 
    }
    }
    }
    myshort('Welcome',2)("To")("Findnerd");
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: