0

i have this code , don't get me wrong it all work fine

var w = "window" ,
  gg = "gg" ,
  pr = "prototype" ,
  al = "alert" ,
  pi = "parseInt" ,
  st = String ,
  ts = "toString";

st[pr][gg] = function(){return window[this[ts]()];};
w = w[gg]();
w[al](w[pi]("0"));

the problem starts when i replace this piece of code

w = w[gg]();
w[al](w[pi]("0"));

with this one

w[gg]()[al](w[pi]("0"));

now it's not working.

i don't get it , it suppose to get the same result , what is wrong here?

homerun
  • 19,837
  • 15
  • 45
  • 70
  • 1
    write clean code, including meaningful names! – Rene Pot Oct 17 '11 at 22:37
  • @Topener i want to make is as much as i can not readable , but thanks. – homerun Oct 17 '11 at 22:43
  • 1
    @Mor: Why don't you listen to the people who commented on your previous questions (which are somehow all about this topic) and **don't** obfuscate your code by hand but **use a tool** that does this for you? Don't you see that you are going through a lot of trouble with this? – Felix Kling Oct 17 '11 at 22:46
  • @FelixKling: *"Why don't you listen to the people..."* Because he's brilliant. Can't you tell? – user113716 Oct 17 '11 at 23:03
  • @Ӫ_._Ӫ (patrick ;)): My brilliance detector is broken... – Felix Kling Oct 17 '11 at 23:06
  • @Mor Sela: if your code is really important, then it's probably tricky and doesn't need obfuscation. Those that want to figure out what the obfuscated code does probably already know how to do it, and those that don't probably really want to learn and will find out anyhow. All-in-all it's not too helpful – vol7ron Oct 17 '11 at 23:10
  • @FelixKling I've entered javascript "hard core" only 2 days ago , i'm trying to learn as much as i can , i just want to challenge a little bit.. – homerun Oct 17 '11 at 23:12
  • I think I already posted this on another question of yours, but again, I really recommend to read: https://developer.mozilla.org/en/JavaScript/Guide . And in all honesty: Trying to write code this way is the wrong challenge. It's more important to understand the concepts behind a language than its syntax and ways to abuse it (IMHO). – Felix Kling Oct 17 '11 at 23:17
  • You could go to http://projecteuler.net and try to solve these problems in JavaScript (though not all might be solvable) if you want challenges (and you'll learn sth about math too ;) (maybe)). – Felix Kling Oct 17 '11 at 23:22
  • 2
    If you're looking for personal challenges, then why are you asking for the solution as soon as you get stuck? If it's really a personal challenge, you should spend a few days banging your head against the wall trying to work it out. Not much of a challenge if someone else provides all the solutions for you. – user113716 Oct 17 '11 at 23:26

1 Answers1

2

There are two places that w is used:

w[al](w[pi]("0"));
^     ^

So you need to substitute in w[gg]() twice:

w[gg]()[al](w[gg]()[pi]("0"));
^^^^^^^     ^^^^^^^

Note also that this transformation might still not equivalent, for example if w[gg]() has side-effects or is non-deterministic.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • oh yeah .. thanks for that. i'm trying to make the code not readable since it's very very valuable for me , is it right to write this way? is it take more time / memory writing this way? and one more question , can i assign as string the method "return" , "this" and "string" and use them as strings just like the rest of the code? if it can be , can i have example? – homerun Oct 17 '11 at 22:40
  • @MorSela: The word you are looking for is "obfuscation". See here: [How can I obfuscate JavaScript?](http://stackoverflow.com/questions/194397/how-can-i-obfuscate-javascript) – Mark Byers Oct 17 '11 at 22:43
  • writing that way is may be a waste of memory / page speed load? or it pretty much similar? – homerun Oct 17 '11 at 22:52
  • Something that should be equivalent would be `(w=w[gg]())[al](w[pi]("0"));`. – icktoofay Oct 17 '11 at 22:52
  • 1
    @Mor: It's mainly a waste of time... and in one month (or probably week), when you have to change something, you will struggle to understand what's actually going on there. – Felix Kling Oct 17 '11 at 22:53
  • @FelixKling thank you felix for the advice but i prefer the code this way because it is very valuable , i have a question if you don't mind , writing that way may be a waste of memory or may effect on page speed load? or it pretty much similar to "normal code"? – homerun Oct 17 '11 at 23:02
  • @Mor: Especially then you should use stable tools instead of doing something error prone yourself. It's like you wanted to implement/invent your own encryption scheme, which is so wrong to do. Use existing tools/algorithms have been developed by more knowledgeable people than you and me and have been tested over and over again. In any case I hope one day you will understand. As for your question, that depends on what the code is doing. – Felix Kling Oct 17 '11 at 23:09
  • can you give me link to the tool you talk about? and thank you for letting me understand. – homerun Oct 17 '11 at 23:14
  • @Mor: https://code.google.com/closure/compiler/ is a good start (though there might be better obfuscators, don't know). It has different levels of optimization. – Felix Kling Oct 17 '11 at 23:15