Difference between reference and instance in javascript

元气小坏坏 提交于 2019-11-28 19:15:59

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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



回答6:


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.




回答7:


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

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




回答8:


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.



来源:https://stackoverflow.com/questions/6395754/difference-between-reference-and-instance-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!