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.
, new MyConstructor() , .
new MyConstructor()
new MyConstructor().someMethod();
... , :)
- , , .
, , , .
, , , :
var myHandlers = {}; function Handler(name) { myHandlers[name] = this; } new Handler("test");
- , , , .
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.