0

I want to alert is it executed well? after following line is executed in javascript

window["is"]("it")("executed")("well")("?")

window here means a global object. I have no idea what that above line is in javascript.

3
  • 1
    Is that actually valid JavaScript? Have you tried executing it to see what happens? Commented Dec 6, 2012 at 9:39
  • 2
    For what it's worth, this is valid Javascript (functions can return functions), but I'm at a loss to explain why something so convoluted would be written... Commented Dec 6, 2012 at 9:41
  • I'm trying to create a recursive function for this, but failing miserably. Can't check if a function will be executed, from within the function, of course... Commented Dec 6, 2012 at 9:46

3 Answers 3

4
window.is = function(it){
    return function(executed){
        return function(well){
            return function(questionMark){
              alert("is "+it+" "+executed+" "+well+" "+questionMark);
            }
        }
    }
}

window["is"]("it")("executed")("well")("?")​

strange question. there's probably a more efficient way of doing it...

demo: http://jsfiddle.net/jd3uM/

Sign up to request clarification or add additional context in comments.

5 Comments

I like yours a bit more since you didn't use is as a string when defining the function. But yeah, this is a super weird question.
It works thanks. Could you explain what this line means in js?
Also, how funny is it that we used exactly the same parameter names lol.
@x4f4r: window.is Is a function that returns a function that returns a function that returns a function. These functions all accept parameters, which are being concatenated in the "deepest" function. Still following? No? That's why window["is"]("it")("executed")("well")("?")​ is a bad idea.
@ahren It is a question which was asked by someone(interviewer) from me. I could not answer this question at that time but now wanted to know what it is and how to solve it. :(
3

Evil recursion :)

arguments.callee refers to the function you are currently calling.

window.is = (function(len){
    var buffer = ["is"];
    return function(str) {
        buffer.push(str);
        if(buffer.length === len) {
            alert(buffer.join(" "));
        }
        else {
            return arguments.callee;            
        }
    }
}(5));

http://jsfiddle.net/tarabyte/wf8ag/

3 Comments

It works too. Could you please tell me what is arguments.callee? Also could you explain your approach. Thanks
You actually made it work recursively, nice! Only too bad you have to supply it's length.
I agree. Nifty solution for a terrible problem.
0

The following works, although I can't think of any possible use for something that ugly.

window["is"] = function (it) {
   return function (executed) {
      return function (well) {
          return function (questionMark) {
              alert("is " + it + " " + executed + " " + well + questionMark);
          }
      }
   }
}

The first thing is to add the is element to the window array (oh my…) and then keep returning functions that will be called.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.