javascript javascript中引用和实例的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6395754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between reference and instance in javascript
提问by James Fair
Sometimes I hear people say "a reference to a object" and some say "a instance of a object" What is the difference?
有时我听到人们说“对一个对象的引用”和一些人说“一个对象的实例”有什么区别?
采纳答案by Oded
A variablewill hold a referenceto an instanceof an object.
甲变量将举行参考到一个实例的一个对象。
The actual object is an instance.
实际对象是一个实例。
The variable/variables used to access the object hold the referencesto it.
用于访问对象的变量/变量保存对它的引用。
回答by Jon
Instance of an object(or, perhaps more accurately phrased when talking about languages that have the notion, of a class) is an object that has been created and exists in memory. For example, when writing
对象的实例(或者,在谈论具有类概念的语言时,更准确地说)是已创建并存在于内存中的对象。例如,当写
var obj = new Foo();
Then a new instance of an object has been created (with new Foo
).
然后创建了一个对象的新实例(使用new Foo
)。
Reference to an objectis some kind of handle that allows us to access an instance. For example, in many languages (including JavaScript) obj
now holds a reference to the instance we just created.
对对象的引用是某种允许我们访问实例的句柄。例如,在许多语言(包括 JavaScript)中,obj
现在都持有对我们刚刚创建的实例的引用。
There can be many references to the same instance, as in
同一个实例可以有多个引用,如
var obj2 = obj;
where now both obj
and obj2
hold references to the same object.
现在哪里都obj
并obj2
保持引用同一个对象。
回答by MattSturgeon
In javascript a variable is a reference to the actual instance
在 javascript 中,变量是对实际实例的引用
// create a new object from the Object constructor
// and assign a reference to it called `firstObjRef`
var firstObjectRef = new Object();
// Give the object the property `prop`
firstObjRef.prop = "im a property created through `firstObjRef`";
// Create a second reference (called `secondObjRef`) to the same object
var secondObjRef = firstObjRef;
// this creates a reference to firstObjRef,
// which in turn references the object
secondObjRef.otherProp = "im a property created through `secondObjRef`";
// We can access both properties from both references
// This means `firstObjRef` & `secondObjRef` are the same object
// not just copies
firstObjRef.otherProp === secondObjRef.otherProp;
// Returns true
This will also work if you pass a variable to a function:
如果您将变量传递给函数,这也将起作用:
function objectChanger (obj, val) {
// set the referenced object;s property `prop` to the referenced
// value of `val`
obj.prop = val;
}
// define a empty object outside of `objectChanger`'s scope
var globalObject = {};
globalObject === {}; // true
// pass olobalObject to the function objectChanger
objectChanger(globalObject, "Im a string");
globalObject === {}; // false
globalObject.prop === "Im a string"; // true
回答by genobis
There can be many references to one instance. It's like, um, many paths leading to the same place.
一个实例可以有多个引用。就像,嗯,很多条路通向同一个地方。
var x="string object";
var y=x;
Both x
and y
are two different (but equal) references to the same instance of an object.
既x
和y
只是对一个对象的同一实例的两个不同(但相等)的引用。
回答by Aliostad
We always use a reference to an object and cannot use the object directly, we can only use the reference. Object instance itself is in memory.
我们总是使用对对象的引用,不能直接使用对象,只能使用引用。对象实例本身在内存中。
When we create an object, we get a reference. We can create more references:
当我们创建一个对象时,我们会得到一个引用。我们可以创建更多引用:
var obj = {}; // a reference to a new object
var a = obj; // another reference to the object
回答by Vikky
Object is an occupied memory for a class. Reference points to that memory and it has got a name (u can call it as a variable). For example, A a = new A(); here when we write "new A();" some memory will be occupied in the system. 'a' is the reference(variable) which points to this memory and is used to access the data present in this memory.
对象是一个类占用的内存。引用指向该内存并且它有一个名称(您可以将其称为变量)。例如,A a = new A(); 在这里,当我们写“new A();” 系统会占用一些内存。'a' 是指向该内存并用于访问该内存中存在的数据的引用(变量)。
Object is obtained when it has a life, means it has occupied some memory. Reference points to the Object. Instance is the copy of the Reference that points to object at a point of time.
对象有生命时才获得,意味着它已经占用了一些内存。引用指向对象。实例是在某个时间点指向对象的引用的副本。
Refrence is a variable that points the objects. Object is the instance of the class that have some memory and instance is a variable & methods that object have.
Refrence 是一个指向对象的变量。对象是具有一些内存的类的实例,实例是对象具有的变量和方法。
Reference means address of object or variable. Object is instance of class and instance means representative of class i.e object
引用是指对象或变量的地址。对象是类的实例,实例代表类,即对象
Instance is the actual object created at run time.
实例是在运行时创建的实际对象。
回答by Balaji Ganesan
Var a="myObject";
var b=a;
Here, variable a
is an Instanceand variable b
is a Reference
这里,变量a
是一个实例,变量b
是一个引用
回答by Jairo N. Cuadra
The actual English language definition of 'instance' and 'reference.'
“实例”和“参考”的实际英语定义。
instance: an example or single occurrence of something.
实例:某事的一个例子或单次出现。
reference: the action of mentioning.
参考:提的动作。
So taking these two definitions of the actual words into consideration and applying it to a JavaScript scenario you can understand how each concept fits.
因此,考虑到实际单词的这两个定义并将其应用于 JavaScript 场景,您可以理解每个概念如何适应。