0

I'm not exactly sure of the name of what I'd like to do but it goes like this:

Currently, I have a bunch of variables in my javascript context that look like $A126 or $B15.

Before running, I have to load in all 9000 of these variables and their current values so later parts of the code can just reference $xxx for the value.

The preloading is not efficient at all and is causing a bottleneck.

As there is a substantial amount of code that uses this $xxx notation I was wondering if it would be possible to make a universal change to $.xxx where $ is a function that performed a lookup of the value passed to it via what was after the period.

So $.xxx would be analogous to GetItem(xxx)

This is for a javascript environment in C# using clearscript, though I don't think that would impact the answer.

It looks like

   function Field(val){
    var value = val;

    this.__defineGetter__("xxx", function(){
        return value;
    });

    this.__defineSetter__("value2", function(val){
        value = val;
    });
}

var field = new Field("test");


console.log(field.xxx)

---> 'test'

That is almost an example of what I'm looking for. The problem is that I would like to have a general defineGetter that doesn't look for a particular getter by name.

Scottingham
  • 906
  • 2
  • 11
  • 26
  • There is no method that gets called when you try to access a property of an object. Why not make `$` a function, so you can do `$(xxx)` (instead of `$.xxx`)? – gen_Eric Jun 19 '14 at 20:14
  • I'd try to avoid that as much as possible as doing a search and replace thousands of instances of $xxx to $.xxx is much easier than $(xxx), but if that is my only option... – Scottingham Jun 19 '14 at 20:16
  • Note that it would probably also need to be `$('xxx')`. As for the search/replace - you could do that with a regular expression – James Thorpe Jun 19 '14 at 20:16
  • @Scottingham: There's no way to make JavaScript run `GetItem(xxx)` when you access `$.xxx`. – gen_Eric Jun 19 '14 at 20:19
  • @RocketHazmat: can's we make getters using Object.defineProperty that would run a function upon property access? – dandavis Jun 19 '14 at 20:24
  • @dandavis: I don't know. I didn't think you could, but I do recall seeing `get:` and `set:` somewhere in ES6, I think. This might be possible after all. – gen_Eric Jun 19 '14 at 20:28
  • 2
    @Scottingham: Turns out it *is* possible. See this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get and this http://ejohn.org/blog/javascript-getters-and-setters/ – gen_Eric Jun 19 '14 at 20:29
  • you can just say $=window; then use $.A126 to reach the var A126... – dandavis Jun 19 '14 at 20:32
  • @dandavis: that won't work because it assumes I've declared A126 somewhere. The point of this is that I would like to avoid having to do that. $.A126 should kick off a function with 'A126' as the parameter...I'm looking at that example page but applying it is proving difficult at this hour in the day for me. – Scottingham Jun 19 '14 at 20:52

0 Answers0