+ 14
Is 'name' variable works like Cookies or how does it work?
I think that about 'name' variable works odd because if it below alert box or document.write ect will not show undefined. Is 'name' variable special or what? Is 'name' variable works like Cookies or how does it work? You can check my code to understand what I mean if you don't have the answer you can upvote the question to make it popular to make who knows answer. At the first time I imagined it a stupid Question then I tried many times then I posted this question to discuss with others. At the end thank you 💕 https://code.sololearn.com/W57kLiCR845f/?ref=app
11 odpowiedzi
+ 5
BroFarOps ,Luis Febro 🇧🇷 ,and daneillonge it was solved and the real problem was in the first multiple comments so I made it as comment and show a hasOwnPeoperty to explain window.name is true and window.age is false that is because name is a property name of window...........
and thanks Prometheus ¯\_(ツ)_/¯ to remember that.
+ 10
At top level, declaring var name makes the interpreter think it is window.name. To avoid use let or const.
+ 6
So I am going to read more about window properties to make it sure and to find the real answer ;)
+ 5
Oh you remember me that
window.name : Gets or sets the name of the window. <solved>
+ 5
hasOwnProperty is a normal Javascript function that takes a string argument.
When you call shape1.hasOwnProperty(name) you are passing it the value of the name variable (which doesn't exist), just as it would if you wrote alert(name).
You need to call hasOwnProperty with a string containing name, like this: shape1.hasOwnProperty("name").
Refer to similar question and answer:
https://stackoverflow.com/questions/2600085/hasownproperty-in-javascript
+ 4
However I still need more explaining to understand well 😕
+ 3
window.name does exist. That's the reason returns "true" when you call hasOwnProperty. This same property returns "true" for properties assigned with null or undefined primitive values, too. I still do not know the reason.
+ 1
gi
0
Window is just a special object that contains everything. Any variable you create is made in window. Example:
var a = 5;
Same as:
window.a = 5;
window[“a”] = 5;
0
variable are being comment out if you run with variable defined
var name = 'name'
var age = 22
alert(window.hasOwnproperty("name"))
alert(window.hasOwnproperty("age"))
it will all alert true
because whatever we write and run codes are inside javascript module and module is a function which has properties name, undefined etc.
function(name, undefined, ...,){
}
so window has own property call name but not age for more check 'js module mdn'.
or check on youtube programmingwithmosh nodejs module.
- 1
Luis Febro 🇧🇷 unless the property is assigned a class string or value it is the same as the value of 0 or empty.
https://code.sololearn.com/WqDEfT1gJEFh/?ref=app