我正在查看关于MDN的文档,发现Object的属性名称可以是Object(它将被转换为字符串)。 我写了一个简单的代码来测试它并且它工作但稍后更改被指定为主Object的键的obj应该更改结果键(字符串表示)因此主对象属性不应该可以使用新对象访问但是它是可以访问的。
我想知道它如何管理这个javascript?
var objee = {}; var rajat = "me"; var rand = Math.random(); var ob = new Object(); objee[ob] = "hey"; console.log(ob,objee[ob]);//javascript is converting the Object key to string using Object.toString(). ob.name = "rajat"; // Why this property is still accesible if the "ob" has changed now..and resulted string is changed,thus key changed? console.log(ob,objee[ob]);我知道这个问题很混乱。希望你能理解。
I was looking in docs on MDN and found that Object poperty names can be Object(which will be converted to string). I wrote a simple code to test it out and it worked but later changing the obj which is being assigned as a key to main Object,should change the resulted key (string representation) thus the Main object property should not be accesible with new object but It is accesible.
I wanna know how javascript it managing this?
var objee = {}; var rajat = "me"; var rand = Math.random(); var ob = new Object(); objee[ob] = "hey"; console.log(ob,objee[ob]);//javascript is converting the Object key to string using Object.toString(). ob.name = "rajat"; // Why this property is still accesible if the "ob" has changed now..and resulted string is changed,thus key changed? console.log(ob,objee[ob]);I know this is question is very confusing..Hope you will understand.
最满意答案
该对象可能已更改,但其字符串表示形式不具有:
var ob = new Object(); console.log(ob.toString()); ob.name = "foo"; console.log(ob.toString());...所以它引用的属性名称也不会改变。
The object might have changed, but its string representation has not:
var ob = new Object(); console.log(ob.toString()); ob.name = "foo"; console.log(ob.toString());… so the property name being referenced by it is also unchanged.
更多推荐
发布评论