[英]To what types do the references get assigned instead of being copied in Javascript?
I am on my mobile phone, so I can't test for myself. 我正在使用手机,因此无法自行测试。 Also, I might miss something.
另外,我可能会错过一些东西。
I know that when a number is being assigned, say a = b, b is actually copied into a. 我知道,当分配一个数字时,例如a = b,b实际上会复制到a中。 But, if b was an object, just a reference wouls be passed.
但是,如果b是一个对象,则只能传递一个引用。 What with other types?
其他类型呢? Is there something I should worry about?
我应该担心什么吗?
Also, I heard that you can't use pointers in C# because variables get moved around by the JC. 另外,我听说您不能在C#中使用指针,因为JC会移动变量。 Is it the same in Javascript?
Javascript是否相同? How are these references solved in these languages then?
那么如何用这些语言解决这些引用?
javascript According to specification, you have types: undefined, null, boolean, string, number, object. javascript根据规范,您有以下类型:undefined,null,boolean,string,number,object。 You can think of them as immutable except object, which is practically just a (hash)map.
您可以将它们视为对象之外不变的对象,而对象实际上只是一个(哈希)映射。 So yes, if you assign variable, it's "copied" (you don't care if it really is) unless it's an object type.
因此,是的,如果您分配变量,则除非它是对象类型,否则它将被“复制”(实际上并不关心它)。 Take example:
举个例子:
var x = "hello";
var y = x; //copy or reference, who cares?
x += " world"; //new string object, x references it
alert(y); //alerts hello
C# According to C# 2.0 specification, there are struct/value types and class/reference types . C#根据C#2.0规范,有struct / value类型和class / reference类型 。 So, from practical point of view, variable of value type actually stores the data on the call stack (and gets copied on assignment), and variable of reference types is just a reference (and data goes to heap).
因此,从实际的角度来看,值类型的变量实际上将数据存储在调用堆栈中(并在分配时被复制),而引用类型的变量仅是一个引用(数据进入堆)。 Example:
例:
int holds_a_value = 5;
StringBuilder holds_a_reference = new StringBuilder();
You can use pointers in C# (pointer = reference), but you have to pin them if you call unsafe functions outside .net / C# , or using unsafe code. 您可以在C#中使用指针 (pointer = reference),但是如果在.net / C#之外调用不安全的函数或使用不安全的代码,则必须固定它们。 eg:
例如:
fixed (int* p = something) { /*p is safe to use, memory doesn't move */}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.