Pull variable name through to constructor on object instantiation

Panomosh

Here's an example constructor

function AnObject(a, b){
    this.a = a;
    this.b = b;
}

and an instantiation of it

var example = new AnObject('test', 'test');

Lets say that the constructor had a property .name

is there anyway to build the constructor so that it sets the object's name to the name of the variable that it was instantiated with?

In the case above, the name would be example

-EDIT-

The reason I am trying to achieve this is to be as "D.R.Y" as possible.

I would like to NOT have to:

var example = new ObjectWithName('example');
John Slegers

No. I'm afraid that's not possible!

You see, the statement var example = new AnObject('test', 'test'); is evaluated from right to left. So, first new AnObject('test', 'test') is evaluated. Then, the value it returns is assigned to var example.

Because of this right-to-left evaluation, there is no way your AnObject constructor can know that its return value is going to be assigned to variable example when it's being evaluated.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Copy constructor Class instantiation

From Dev

Object instantiation fails when using overloaded constructor

From Dev

JS run function from constructor after object instantiation

From Dev

Loop through each new Object from Constructor

From Dev

typescript, How do I pass an object to a constructor of class for instantiation

From Dev

Does the construction of an object through a class variable always need the parent class to have a constructor?

From Dev

Get constructor name of object

From Dev

Object instantiation through factory method not giving desire result

From Dev

Avoid indirect instantiation from private constructor through operation

From Dev

Object changed when passed through a constructor

From Dev

Get class's property name at compile time without object instantiation

From Dev

Object name as variable in Logtalk

From Dev

How to access variable name assigned to object from within class constructor in Javascript?

From Dev

Using constructor parameter variable names during object instantiation in Python?

From Dev

C++ Object Instantiation - Which constructor is called when an object is instantiated using empty parenthesis

From Dev

Storing HTML Form input as an Object using a Constructor class and a dynamic variable name (Plain JS)

From Dev

Deserialize into object with variable name

From Dev

Array instantiation calls a constructor?

From Dev

Passing an object through a constructor?

From Dev

Object instantiation: Inner class and outer class with the same name (Java)

From Dev

Javascript: Using a variable as a constructor name

From Dev

New object with name in variable

From Dev

Object Instantiation Calling Constructor Too Many Times

From Dev

How to define instantiation of object class as global variable in Python?

From Dev

Dynamic variable name in object

From Dev

How to write a SQL query to pull a value from a nested json object identified by a variable field name

From Dev

How Do I Set a Breakpoint for Object Instantiation, with No Constructor?

From Dev

Class variable shows no data after instantiation inside prefab object

From Dev

Loop through Viewbags with a variable name

Related Related

  1. 1

    Copy constructor Class instantiation

  2. 2

    Object instantiation fails when using overloaded constructor

  3. 3

    JS run function from constructor after object instantiation

  4. 4

    Loop through each new Object from Constructor

  5. 5

    typescript, How do I pass an object to a constructor of class for instantiation

  6. 6

    Does the construction of an object through a class variable always need the parent class to have a constructor?

  7. 7

    Get constructor name of object

  8. 8

    Object instantiation through factory method not giving desire result

  9. 9

    Avoid indirect instantiation from private constructor through operation

  10. 10

    Object changed when passed through a constructor

  11. 11

    Get class's property name at compile time without object instantiation

  12. 12

    Object name as variable in Logtalk

  13. 13

    How to access variable name assigned to object from within class constructor in Javascript?

  14. 14

    Using constructor parameter variable names during object instantiation in Python?

  15. 15

    C++ Object Instantiation - Which constructor is called when an object is instantiated using empty parenthesis

  16. 16

    Storing HTML Form input as an Object using a Constructor class and a dynamic variable name (Plain JS)

  17. 17

    Deserialize into object with variable name

  18. 18

    Array instantiation calls a constructor?

  19. 19

    Passing an object through a constructor?

  20. 20

    Object instantiation: Inner class and outer class with the same name (Java)

  21. 21

    Javascript: Using a variable as a constructor name

  22. 22

    New object with name in variable

  23. 23

    Object Instantiation Calling Constructor Too Many Times

  24. 24

    How to define instantiation of object class as global variable in Python?

  25. 25

    Dynamic variable name in object

  26. 26

    How to write a SQL query to pull a value from a nested json object identified by a variable field name

  27. 27

    How Do I Set a Breakpoint for Object Instantiation, with No Constructor?

  28. 28

    Class variable shows no data after instantiation inside prefab object

  29. 29

    Loop through Viewbags with a variable name

HotTag

Archive