In yesterday's "Authorited Javascript Guide" study notes 10: ECMAScript 5 Enhanced Object Model, a strange phenomenon occurred when debugging a piece of code. Now post the source code below:
The code copy is as follows:
<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/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
This result is correct, but if you change it to another place, the result will be different
The code copy is as follows:
<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>
Running results:
Why is this? Just changed a simple variable name
The problem lies with name, but I don’t understand it. I searched for information online but had no results. Later, I went to ask a senior brother and put the problem on the forum: http://bbs.csdn.net/topics/390799744?page=1#post-397474060,
name is a window property, which is used to set or return the name of the window, and the data type is a string. Because the first sentence is function Person(){}, it has ended, so the following code may be in the global context, that is, you think alert(JSON.stringify(name)); is the above var name output, but in fact, it may be window.name output. As for output [Object object], this type is the type of the value of name.