Om3ga
Om3ga

Reputation: 32883

Need to run alert a string using javascript code

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.

Upvotes: 0

Views: 118

Answers (3)

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

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/

Upvotes: 3

Ingo Bürk
Ingo Bürk

Reputation: 20043

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.

Upvotes: 0

ahren
ahren

Reputation: 16959

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/

Upvotes: 4

Related Questions