This question already has answers here:
How can I unset a JavaScript variable?
(12 answers)
Closed 7 years ago.
I have declare global variable in java script, i try to clear with following code but it not clear.
As i defined global variable.
var usno;
then before set value i tried following code
delete window.usno;
usno = undefined;
usno=null;
but no success.
Please help on this
Thanks
Basit.
In short, you cannot delete variables defined with var. If you set the variable using window.usno then it can be. See Understanding delete and the answers to How to unset a JavaScript variable? for more details.
Do you really need to delete the variable though? Instead of making a global variable, restrict the scope of your variable to a function so that it will be garbage collected after the scope of the function ends.
Your first solution is the correct one.
delete window.usno;
That deletes the property usno from the global window object. But here is where the confusion lies, when we test it:
window.usno; // -> undefined
So, a deleted or non-existent property returns undefined. Thus, in effect, the following two statements achieve much the same thing:
delete window.usno;
window.usno = undefined;
They are not exactly the same thing. In the second case, the usno property still exists, as you will find if your iterated through all of the properties of window.
Short answer:
delete window.usno;
Related
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 2 years ago.
I'm a beginner and following a long from a udemy course and I've come across this example where we use the THIS keyword to reference the properties within it's object. However, I don't understand why we can't just grab the property like we would normally do when we are outside of the object by doing: objectname.property. I tested it and it seems to work fine. I'm obviously missing something so if someone could let me know that would be hugely appreciated! The example is below. Instead of using this, why not just use mark.bmi for instance.
const mark = {
name: `mark`,
mass: 92,
height: 1.95,
bmiCalc: function() {
this.bmi = this.mass / (this.height * this.height);
return this.bmi;
}
}
Properties can be added dynamically to an object. You presented a trivial case, but in most of the cases, the compiler would not be able to decide without a "this." if you reference a member of the current object, or some global variable
Change the variable name mark to jacob, you don't need to refactor. But if you had changed this to mark then you needed to change it also to the new variable name.
Usually you create more objects (or instances) from a class. The method defined in the class applies to more than just this object (mark). That's the reason for using this in object oriented programming.
This question already has an answer here:
why referencing non-existent property of an object in javascript doesn't return a reference error?
(1 answer)
Closed 5 years ago.
I was playing with variables, when i encountered this. I could not find out a reason for this. I want to understand this.
Below gives an exception for reference error which is obvious:
console.log(age);
but when the same thing is done on an object, it does not throw any kind of error. It prints out 'undefined' as if the variable is already declared:
var person = {};
console.log(person.age)
And interestingly when you check the 'person' object, there is no 'age' property.
I understand we can create property on objects directly like this:
person.age = 3;
and so can be done for global or local variables:
a = 3
But still, accessing something before assigning or declaring should throw an exception or error just like it happens in case of global or local scope variables.
Below gives an exception for reference error which is obvious
Yes, because there is no reference age.
but when the same thing is done on an object, it does not throw any
kind of error.
Why would it? You're referencing an object through valid reference person, and since this object doesn't have the property age the value undefined is returned.
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
Say for example you have a function like below
var value = 1;
function test(myVar) {
myVar = 2
}
test(value)
console.log(value)
//prints out 1
Now, I have read that there are two things that happen in JS functions.
1) Javascript passes primitives by value. When "value" is passed as an argument to test(), a copy is created that is separate from the global variable "value". Therefore the "myVar" is a separate variable from "value"
2) Parameters in JS functions are really just local variables to the function. So, the "myVar" parameter has scope only inside of the test() function.
I know both statements are true, but which one of these statements causes console.log(value) to print out 1
Both of those statements are true. The reason why it prints out 1 is that, as you mentioned, parameters are passed by value. As such, the value of your variable value is never altered.
Although point #2 is also true, it doesn't really have much to do with this behaviour. it is more related to doing something like using myVar outside of that function's scope.
Both of the statements are correct
Please check this URL from Mozilla Variable Scope in Javascript
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I have an object and a few variables I'd like nested inside the variable but for some reason I can only ever get the first nest. Anything after that gives me an error stating that it couldn't read because it was undefined:
var date = 10;
var timestamp = 100;
Que[date] = {timestamp:{"test":"test"}};//this returns {10:{"timestamp":{"test":"test"}}}; for some reason
Que[date][timestamp] = {"test":"test"}; //errors saying, cannot read '100' undefined
console.log(Que);
I'm not sure why this is happening and I'd really like to resolve it with simple means. Btw the Que variable is global inside another script and predeterminietly contains {"10":{"24":{"1":"test"}}}; which is likely why the date variable does work but the timestamp variable doesn't. Any suggestions?
EDIT:
date, and timestamp are both declared outside of the object, however i wish to use them as key's inside of the object..
uppon reading the mod suggested post and implementing what it contained, i ended up with another error
Que = {[date]:{[timestamp]:{"test":"test"}}};//this results in an unexpectd token error located at the [ before date
I think there are more questions to be asked based off of your code snippet, but in your example timesheet is not defined, you can reference the key inside your JSON by using
Que[date]["timesheet"]
To access the object inside. The key that you're looking up with the [] notation should be a string.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript how do you find the caller function?
Is there a way to get the value of this from the function which has called the current function?
Look at this:
function TraceMySelf(){
console.log(this);
}
function A(){
TraceMySelf();
console.log(this);
}
var a = new A();
When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.
Is this possible?
I think this is the answer to your question: StackOverflow 280389
However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.
It might also be worth looking at jQuery Proxy for another way of linking function and object.