Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript defineGetter

var a = {}; 

a.__defineGetter__('test',function() {return 5;}); 

var i ="test";  

Is there any other way I can execute the getter besides a[i] (while using var i to do it)

EDIT:

I was asking ways to use var i to do it. I'll explain the real problem a bit better.

I am using getters on my namespace object to load modules only when needed.

MyNameSpace.__defineGetter__('db',function(){MyNameSpace.loadModule('db');});

In this case I am trying to load all modules:

for (var i in MyNameSpace){
    MyNameSpace[i];
}

I use Google closure compiler on my code, and it reduces that loop above to:

for(var i in MyNameSpace);

No modules get loaded. I am trying to "trick" gcc into letting me load the modules.

like image 687
Dr Heiter Avatar asked Sep 03 '10 00:09

Dr Heiter


People also ask

What is a getter JavaScript?

Getters give you a way to define a property of an object, but they do not calculate the property's value until it is accessed. A getter defers the cost of calculating the value until the value is needed.

What is setter and getter in JavaScript?

In JavaScript, accessor properties are methods that get or set the value of an object. For that, we use these two keywords: get - to define a getter method to get the property value. set - to define a setter method to set the property value.

How is an object property referenced in JavaScript?

Object properties are defined as a simple association between name and value. All properties have a name and value is one of the attributes linked with the property, which defines the access granted to the property. Properties refer to the collection of values which are associated with the JavaScript object.

What is __ proto __ JS?

__proto__ is a way to inherit properties from an object in JavaScript. __proto__ a property of Object. prototype is an accessor property that exposes the [[Prototype]] of the object through which it is accessed. POSTly is a web-based API tool that allows for fast testing of your APIs (REST, GraphQL).


2 Answers

You can do either a.test or a['test'] - both will access the test property of a and hence call the getter.

Edit: Ah, I see exactly what you want now. What you're doing is a clever use of getters, but unfortunately getters and setters aren't part of the current JavaScript standard (they are in ECMAScript 5 which isn't widely supported yet). Google Closure Tools seems to assume that reading a variable can't have any side-effect, which is true in the current versions of JavaScript, so I see no way to get around that. You'll have to edit the output to insert that stuff back.

Also, this isn't related to your question, but I do hope you're doing an additional hasOwnProperty check within the for-in construct.

like image 187
casablanca Avatar answered Oct 20 '22 11:10

casablanca


I guess closure compiler optimizes out the code because it doesn't actually do anything but access the properties. this should work:

module = {}; // global
for (var i in MyNameSpace){
    module = MyNameSpace[i];
}
like image 36
J.C. Inacio Avatar answered Oct 20 '22 10:10

J.C. Inacio