A sample introduction to the meaning conflict of names in JavaScript
- 2020-03-30 03:09:35
- OfStack
In yesterday's authoritative guide to Javascript study note 10: the object model enhanced by ECMAScript 5, there was a strange phenomenon with the debugging of a piece of code.
Running results in Google:
http://img.blog.csdn.net/20140529073008296? Watermark / 2 / text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTA0Mzg0Mw = = / font / 5 a6l5l2t/fontsize / 400 / fill/I0JBQkFCMA = = / dissolve / 70 / gravity Center
This result is correct, but change one place, the result is different
Operation results:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/201405291449142.gif? 2014429144927 ">
Why is that? I just changed a simple variable name
The problem on the name, but I in JS, do not understand, no results found on the Internet, went to consult a brother behind, also put problems in the BBS on: http://bbs.csdn.net/topics/390799744? Page = 1 # post - 397474060,
Name is the window property that sets or returns the name of the window, and the data type is a string. Since the first sentence is function Person(){}, which is over, the following code is probably in the global context, which means that you think alert(json.stringify (name)); The output is the var name above, but it might actually be window.name. As for the output [Object Object], this type is the type of the value of the name.
<script type="text/javascript">
function Person(){}
var per = new Person;
Object.defineProperties(per,
{
"nickName":
{
value:"Tom",
writable:true
},
"age":
{
value:20,
configurable:false,
writable:false
}
});
var o = Object.getOwnPropertyDescriptor(per,"nickName");
alert(JSON.stringify(o));
</script>
Running results in Google:
http://img.blog.csdn.net/20140529073008296? Watermark / 2 / text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTA0Mzg0Mw = = / font / 5 a6l5l2t/fontsize / 400 / fill/I0JBQkFCMA = = / dissolve / 70 / gravity Center
This result is correct, but change one place, the result is different
<script type="text/javascript">
function Person(){}
var per = new Person;
Object.defineProperties(per,
{
"nickName":
{
value:"Tom",
writable:true
},
"age":
{
value:20,
configurable:false,
writable:false
}
});
var name = Object.getOwnPropertyDescriptor(per,"nickName");
alert(JSON.stringify(name));
</script>
Operation results:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/201405291449142.gif? 2014429144927 ">
Why is that? I just changed a simple variable name
The problem on the name, but I in JS, do not understand, no results found on the Internet, went to consult a brother behind, also put problems in the BBS on: http://bbs.csdn.net/topics/390799744? Page = 1 # post - 397474060,
Name is the window property that sets or returns the name of the window, and the data type is a string. Since the first sentence is function Person(){}, which is over, the following code is probably in the global context, which means that you think alert(json.stringify (name)); The output is the var name above, but it might actually be window.name. As for the output [Object Object], this type is the type of the value of the name.