简体   繁体   English

JavaScript对象的奇怪行为

[英]Strange behaviour with JavaScript objects

This is a strange thing I have noticed, I don't remember it ever being a problem in the past. 我注意到这是一件奇怪的事情,我不记得它曾经是一个问题。

Within Google Chrome (47.0.2526.106 m) objects seem to not be passed to other variables as they should. 在Google Chrome浏览器(47.0.2526.106 m)中,对象似乎没有传递给其他变量。 So below is a snippet of code I have been testing this with and the expected result: 因此,下面是我一直在测试的代码片段以及预期的结果:

Expected result 预期结果

var myobj = {
    count: 5
};

console.log(myobj) // will log something like object(count: 5)
console.log(myobj['count']) // 5

var anotherobj = myobj;

console.log(anotherobj) // will log something like object(count: 5)
console.log(anotherobj['count']) // 5

The above is what I remember being the case as objects are passed as references so you still get the data but under another 'name', what seems to actually happen is below: 上面是我记得的情况,因为将对象作为引用传递,所以您仍然可以获取数据,但是使用另一个“名称”,实际发生的情况如下:

**Actual result* **实际结果*

var myobj = {
    count: 5
};

console.log(myobj) // will log something like object(count: 5)
console.log(myobj['count']) // 5

var anotherobj = myobj;

console.log(anotherobj) // [object Object] <- this seems to be a string
console.log(anotherobj['count']) // undefined

I don't see what is happening, is this a bug is the recent version of Chrome or am I just having some strange imaginative thought thinking that this used to be possible?? 我不知道发生了什么,这是最新版本的Chrome还是一个bug?或者我只是以某种奇怪的想象力认为这曾经是可能的?

Thanks for your help in advanced :) 感谢您对高级的帮助:)

I just had another look and I think I made a typo in my code somewhere. 我只是换了个样子,我想我在代码中的某个地方打错了字。 I used what I wrote in my question and it worked fine. 我用我在问题中写的内容,效果很好。 Been trying to work this out for about half an hour. 试图解决这个问题大约半小时。 How did I not see that it was just a typo?? 我怎么没看到这只是一个错字?

Think I need sleep :P 认为我需要睡觉:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM