javascript中引用和实例的区别
有时我听到人们说“对象的引用”,有些人说“对象的实例” 有什么区别?
Sometimes I hear people say "a reference to a object" and some say "a instance of a object"
What is the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
变量将保存对对象实例的引用。
实际对象是一个实例。
用于访问对象的一个或多个变量保存对其的引用。
A variable will hold a reference to an instance of an object.
The actual object is an instance.
The variable/variables used to access the object hold the references to it.
对象的实例(或者,在谈论具有类概念的语言时可能更准确地表达)是已创建并存在于内存中的对象。例如,当编写
Then 时,已经创建了一个对象的新实例(使用
new Foo
)。对对象的引用是某种允许我们访问实例的句柄。例如,在许多语言(包括 JavaScript)中,
obj
现在保存对我们刚刚创建的实例的引用。可以有多个对同一实例的引用,例如
现在
obj
和obj2
都保存对同一对象的引用。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
Then a new instance of an object has been created (with
new Foo
).Reference to an object is 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.There can be many references to the same instance, as in
where now both
obj
andobj2
hold references to the same object.一个实例可以有多个引用。就像,嗯,很多条路都通向同一个地方。
x
和y
都是对同一对象实例的两个不同(但相等)的引用。There can be many references to one instance. It's like, um, many paths leading to the same place.
Both
x
andy
are two different (but equal) references to the same instance of an object.我们总是使用对象的引用,不能直接使用对象,只能使用引用。 对象实例本身位于内存中。
当我们创建一个对象时,我们会得到一个引用。我们可以创建更多参考:
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:
对象是一个类占用的内存。引用指向该内存并且它有一个名称(您可以将其称为变量)。例如,
A a = 新A();
当我们写“new A();”时系统中会占用一些内存。
“a”是指向该内存的引用(变量),用于访问该内存中存在的数据。
当对象有生命时就获得了,意味着它已经占用了一些内存。
指向对象的引用。
实例是指向某个时间点对象的引用的副本。
引用是指向对象的变量。对象是具有一定内存的类的实例,实例是变量和实例。对象具有的方法。
引用是指对象或变量的地址。
对象是类的实例,实例意味着类的代表,即对象
实例是在运行时创建的实际对象。
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.
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.
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.
在 javascript 中,变量是对实际实例的引用,
如果将变量传递给函数,这也将起作用:
In javascript a variable is a reference to the actual instance
This will also work if you pass a variable to a function:
“实例”和“参考”的实际英语定义。
实例:某事物的示例或单次出现。
引用:提及的动作。
因此,考虑到实际单词的这两个定义并将其应用到 JavaScript 场景中,您可以理解每个概念如何适合。
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.
这里,变量
a
是一个实例,变量b
是一个引用Here, variable
a
is an Instance and variableb
is a Reference