What happens if a new instance object is not assigned to a variable?

What happens if you do not assign a new object to a variable? For instance:

function MyConstructor() {
   // Codes here
};

new MyConstructor(); // new object instance is not assign to a variable

Is this code dangerous? Does this smooth the global namespace? Is it possible to access an object created using this style?

Thank.

+5
source share
3 answers
  • Is this code dangerous? - Nope .
  • Does the global namespace hide? - Nope .
  • Is it possible to access an object created using this style? - Nope .

, new MyConstructor() , .

new MyConstructor().someMethod();

... , :)

+8

- , , .

, , , .

, , , :

var myHandlers = {};

function Handler(name) {
  myHandlers[name] = this;
}

new Handler("test");

- , , , .

+4

because after its creation there is no reference to it, garbage will be collected soon.

FYI only: the reason for creating objects is 80 to 90% of the time, so they can be referenced and used later.

+1
source