This question already has an answer here:
Can't get global variables inside the function (javascript)
(1 answer)
Closed 8 years ago.
Well, after much head scratching this afternoon; I came to realize why my array was coming back as undefined. Despite the word 'closed' not being a reserved JS word; it seems it is some kind of reserved word elsewhere and so an array cannot be called 'closed'.
My question is this; if Javascript isn't reserving this word - what is? The browser? The OS? I read that one should avoid using it as a naming convention for variables / objects but I don't understand what else is trying to use it.
Any insight would be much appreciated.
<html>
<head>
</head>
<body>
<script>
var greatArray = [];
var closed = [];
alert(greatArray.length);
alert(closed.length);
</script>
</body>
</html>
It's not so much that Javascript has reserved it as that it is reserved because of where Javascript is used. Consider this:
if (myWindow.closed)
Therefore, using closed as a name for a global variable must be avoided. You can use it as a local variable, though.
As mentioned by T.J. Crowder:
It's a predefined property on window that you can't redefine, and since all properties on window are globals
Related
This question already has answers here:
How do I create dynamic variable names inside a loop?
(8 answers)
Create dynamic variable names based on count result
(3 answers)
Closed 3 months ago.
I am trying to create dynamic variables using Window. The exact thing I want to do is working fine with eval. But as you may know, that’s not advised.
Some examples.
I may define some variables like this.
var word1 = The;
var word2 = Number;
var word3 = Is;
var number = 100;
Now I combine them.
var phrase = word1+word2+word3+number.
If I use the window keyword in front of the phrase variable, then it will work, provided it is at the beginning of the line of code. However, if I try to put window in front of a variable in the middle of a line of code, it doesn’t work. However, eval does work. Example below.
window[phrase]
That will work.
But if it’s something like this
window[newCombinedVariable] = document.querySelector(window[newCombinedVariable2])
That will not work.
However, this will work.
window[newCombinedVariable] = document.querySelector(eval(newCombinedVariable2))
So, window can take a combination of strings with dynamic variables and it works at the beginning, but not in the middle, such as after doc/query selector. But eval works fine that way when using the exact same combined variables.
Can someone suggest what I must do in order for window to give me the same results as eval?
This question already has answers here:
Are there pointers in javascript?
(6 answers)
Pointers in JavaScript?
(15 answers)
Closed 3 years ago.
A friend of mine told me JS has the best kind of pointers?
I did a bit of research, and everywhere I looked for memory addresses in JS and pointers I found answers like
Its more or less possible
It's more or less Impossible
So which is it?
How can you create a pointer in JS?
There are no pointers in the JavaScript language.
Every variable that contains an Object is actually an opaque reference to that object.
Within the interpreter that reference will take the form of a pointer, but the value of that pointer is not accessible to you.
Javascript do not support pointers.
But you can make use of Object which will serve the purpose of pointers.
Refer the code below for passing parameters to function by reference :
var numbers = {num1:5,num2:8};
var mod = changeNum(numbers);
function changeNum(var Obj){
Obj.num2 += 2;
}
Output:
{num1:5,num2:10}
This question already has answers here:
Use JavaScript variable as function name?
(5 answers)
Closed 7 years ago.
In GSC, you are able to make a variable become the name of a function that you thread. It looks like this:
variable = "pizza";
[[variable]]();
the engine then reads it like:
pizza();
my question is, is it possible to do that in javascript as easily or do I have to make if/else/switch statements for it?
my question is, is it possible to do that in javascript as easily or
do I have to make if/else/switch statements for it?
If you want to use the safe, fail-proof way, then you can access such variables only in two contexts.
If the variable is in global context, in the case of which, you can do window[variable]();
Else if the variable is a property of an object, in the case of which, you can do obj_name[variable](), basically anything that can be accessed via bracket notation. window is an object too.
Then there's always the dirty way:
You can use highly evil eval like eval(variable + "()") or you can use the Function constructor in the same way. Note however that both the methods can be misused and are highly advised against.
This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 8 years ago.
I'm learning Javascript and I've come across something I don't understand. This is the part of my object code:
var monster =
{
...
//is the animation starting?
hiding: true,
delayDuration: Math.floor(Math.random() * 60),
currentDelay: this.delayDuration,
...
};
If I console.log (delayDuration) I get a value, but if I console.log (currentDelay) it says 'undefined'.
I don't understand why currentDelay doesn't take the value of delayDuration.
Can someone please explain this?
edit: #Bergi why did you mark this as duplicate? I couldn't find my question answered somewhere else
edit2: yup, it's a duplicate. At least now I know the words for what I was asking.
At the point of object creation neither monster nor any of it's properties are defined. You can't use variable from the same object you're in the process of constructing.
Also, Javascript uses function scoping, which means that the value of this will either be the window object or will be scoped to the closest instance you're creating using new (or other instance creation techniques).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?
(function(window, undefined){})(window);
What is the meaning of this code? I have seen it in many documents, especially in jQuery documents.
How does this work, and why is it defined thus?
You are scoping a piece of code..
By.
Defining it within an anonymous function //function(){...}
Executing it. //(function{})(args)
Also, passing the window parameter allows for faster resolution of the meaning of that variable within your block of code.