本文介绍了Javascript中的原始类型/引用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的假设是Javascript中存在原始类型和引用类型。在日常工作中,我从来没有对我产生过影响,但我刚刚开始更多的JS,并希望更新我的'思考'。换句话说,我会打赌20美元,以下将返回68I have lived under the assumption that there are primitive types and reference types in Javascript. On a day-to-day basis, I've never had this impact me but I was just starting to a lot more JS and wanted to update my 'thinking'. In other words, I would have betted $20 that the following would return 68var my_obj = {};var tmp_obj = {};tmp_obj.my_int = 38;my_obj.tmp_val = tmp_obj.my_int;tmp_obj.my_int = 68;alert('68 means reference, 38 means primitve: ' + my_obj.tmp_val);但它返回38.所有数字实例都是原始的类型即使它们存在于引用类型的上下文中?如果你,我真的很惊讶,发现这种奇怪的行为(并且会出20美元)。或者我的例子没有证明我认为它是什么?Are all instances of numbers primitive types even if they exist in the context of a reference type? If y, I'm really surprised and find that odd behavior(and would be out $20). Or is my example not demonstrating what I think it is?提前thx UPDATE# 1哇,谢谢所有答案。这是一个微小的变化,这对我的理解很有帮助:Wow, thx for all the answers. Here's a slight change which helps me a lot in understaning:var my_obj={};var tmp_obj={};var my_obj_2=tmp_obj;tmp_obj.my_int=38;my_obj.tmp_val=tmp_obj.my_int;tmp_obj.my_int=68alert('68 means reference, 38 means primitve: ' + my_obj.tmp_val); // 38alert('68 means reference, 38 means primitve: ' + my_obj_2.my_int); // 68my_obj_2.my_int=78;alert(tmp_obj.my_int); // tmp_obj is now 78 ie two way推荐答案你的例子如果您有 my_obj = tmp_obj;然后,所有属性都会指向相同的引用,因为只有一个对象。Then, all the properties would point to the same reference since there would only be one object.但是当你写的时候 my_obj.tmp_val = tmp_obj.my_int;然后 my_obj.tmp_val 将取值它存储在 tmp_obj.my_int 中,但不会在2个对象之间创建新的引用。then my_obj.tmp_val will take the value that's stored in tmp_obj.my_int but that doesn't create a new reference between the 2 objects. 这篇关于Javascript中的原始类型/引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 06:21