I’ve seen something similar to this code in the Google API JavaScript, I mean the r=Array part. Here is an example of what they have done:
var r = Array;
var t = new r('sdsd' , 'sdsd');
alert(t[0]);
Few questions about this:
- Is it legal to write like this and won’t cause any problems?
- I can do something similar with other keywords like ´For´ loop or with the ´this´ keyword?
- Can I have article about this JavaScript official keyword shortcuts etc..?
Thank you in advance.
for– no.this– yes.You can store references to any JavaScript object in a variable.
String,Array,Object, etc. are JavaScript objects that are built-in to the language.for,if,while, etc. are are JavaScript statements, and cannot be stored or referenced any other way.You can do it the other way around as well (and really mess yourself up in the process):
This is easily undone like this:
Edit: Being able to assign the value of
thisto a variable is essential when nesting functions that will execute in a different scope:Maybe not the best example, but illustrates losing scope.
You cannot reassign the value of
this: