javascript中引用和实例的区别

发布于 2024-11-16 05:09:28 字数 41 浏览 2 评论 0原文

有时我听到人们说“对象的引用”,有些人说“对象的实例” 有什么区别?

Sometimes I hear people say "a reference to a object" and some say "a instance of a object"
What is the difference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

药祭#氼 2024-11-23 05:09:28

变量将保存对对象实例引用

实际对象是一个实例

用于访问对象的一个​​或多个变量保存对其的引用

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.

逆光下的微笑 2024-11-23 05:09:28

对象的实例(或者,在谈论具有类概念的语言时可能更准确地表达)是已创建并存在于内存中的对象。例如,当编写

var obj = new Foo();

Then 时,已经创建了一个对象的新实例(使用 new Foo)。

对对象的引用是某种允许我们访问实例的句柄。例如,在许多语言(包括 JavaScript)中,obj 现在保存对我们刚刚创建的实例的引用。

可以有多个对同一实例的引用,例如

var obj2 = obj;

现在 objobj2 都保存对同一对象的引用。

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).

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

var obj2 = obj;

where now both obj and obj2 hold references to the same object.

隔岸观火 2024-11-23 05:09:28

一个实例可以有多个引用。就像,嗯,很多条路都通向同一个地方。

var x="string object";
var y=x;

xy 都是对同一对象实例的两个不同(但相等)的引用。

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.

咋地 2024-11-23 05:09:28

我们总是使用对象的引用,不能直接使用对象,只能使用引用。 对象实例本身位于内存中。

当我们创建一个对象时,我们会得到一个引用。我们可以创建更多参考:

var obj = {}; // a reference to a new object
var a = obj; // another reference to the 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:

var obj = {}; // a reference to a new object
var a = obj; // another reference to the object
跨年 2024-11-23 05:09:28

对象是一个类占用的内存。引用指向该内存并且它有一个名称(您可以将其称为变量)。例如,
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.

等数载,海棠开 2024-11-23 05:09:28

在 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

如果将变量传递给函数,这也将起作用:


    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

In javascript a variable is a reference to the actual instance

    // 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

御守 2024-11-23 05:09:28

“实例”和“参考”的实际英语定义。

实例:某事物的示例或单次出现。

引用:提及的动作。

因此,考虑到实际单词的这两个定义并将其应用到 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.

转角预定愛 2024-11-23 05:09:28
Var a="myObject";
var b=a;

这里,变量a是一个实例,变量b是一个引用

Var a="myObject";
var b=a;

Here, variable a is an Instance and variable b is a Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文