3

I need to reference A.ns. I was hoping to do:

this.ns

from with in A to access ns but it does not work.

var SStorage = FOO.support({
    Name: 'SStorage',

    // non functions

    A: {
        storage:    null,
        ns:         'FOO_',
        len:        null,            // should hold the length of the string in ns:
        indicator:  'FOO_h_token'
    },

I get the error

this is not defined

from with in A.

4 Answers4

2

You could use js get operator and achieve "property as a function" effect:

...
A: {
 ns : 'asdsad',
 get len(){ return this.ns.length;},
}
...

and now you could use it as a normal property:

A.len which will be always actual.

Here is more details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
  • Why have a method for something that is just directly available as a property with `A.ns.length`? – jfriend00 Jun 04 '13 at 21:41
  • Well in OP example there is annonymous object so we should use `this` rather. Also, I suppose OP is asking for `property` not `function`. – IProblemFactory Jun 04 '13 at 21:44
  • 1
    +1 I wonder if we'll ever be able to use cool syntax like this before the *next* version of JS is released next year :'( – Adam Rackis Jun 04 '13 at 22:00
1

You can't reference a containing object in the kind of data structure you have. Likewise, in a static declaration like you have, you can't reference sibling properties either.

You can define the string at a higher level and then reference it in two places like this:

var strData = 'FOO_';
var SStorage = FOO.support({
    Name: 'SStorage',

    // non functions

    A: {
        storage:    null,
        ns:         strData,
        len:        strData.length,
        indicator:  'FOO_h_token'
    },

One might also wonder why you are passing the length at all, when the caller can simply retrieve the length from the passed in string whenever they want to. It doesn't need to be passed separately.

What I'd actually suggest is that you eliminate the length from the data structure at all and if you want a short way to reference it, then put it in a local variable in the implementation of FOO.support() like this:

var SStorage = FOO.support({
    Name: 'SStorage',

    // non functions

    A: {
        storage:    null,
        ns:         'FOO_',
        indicator:  'FOO_h_token'
    },
});

FOO.support = function(obj) {
    var nsLen = obj.A.ns.length;
    // now you have the length cached locally without requiring the caller 
    // to do extra work
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I use the length property in different methods and wanted one place to access it. Shortens the look-up chain. `FOO.length vs. length.` ... similar to why you would do `length = obj.length` in a for loop. –  Jun 04 '13 at 21:30
  • @pure_code.com - it would be better to add one line of code to the beginning of `FOO.support()` that loads the length into a local variable so you have one place to access it with a shorter lookup chain. Then, this whole issue goes away and the burden of optimizing the code isn't on the caller, but is done once inside the function implementation. – jfriend00 Jun 04 '13 at 21:33
  • @pure_code.com - I added a second option that puts the length optimization into the `FOO.support()` function. – jfriend00 Jun 04 '13 at 21:37
0
len: null,  // should hold the length of the string in ns:

You need to use a method for this:

A: {
        storage:    null,
        ns:         'FOO_',
        indicator:  'FOO_h_token',
        len: function(){ return this.ns.length; }
    }

Which of course you'd call as

A.len();

But, as jfriend00 notes, if this is really all you need, then just ditch len completely and do

A.ns.length;
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • Why have a method for something that is just directly available as a property with `A.ns.length`? – jfriend00 Jun 04 '13 at 21:39
  • @jfriend00 - true. I don't know what the rest of his code looks like, but if he really just needs to get the length of ns, then yeah, A.ns.length would be best. – Adam Rackis Jun 04 '13 at 21:41
-2

The other answer are good, but dont forget the obvious...

var SStorage = FOO.support({
Name: 'SStorage',

// non functions

A: {
    storage:    null,
    ns:         'FOO_',
    len:        'FOO_'.length,            // should hold the length of the string in ns:
    indicator:  'FOO_h_token'
},
gbtimmon
  • 4,238
  • 1
  • 21
  • 36
  • Generally bad practice to repeat the same constant multiple times. The opposite of [DRY](http://en.wikipedia.org/wiki/Don't_repeat_yourself) and sets up a maintenance issue if both aren't always changed at the same time. – jfriend00 Jun 04 '13 at 21:40
  • I was making the assumption that 'FOO_' wasn't really a dry reference but only presented that way for simplitcies sake. The point of the comment was that instead of doind late binding / self-reference, he should just evaluate at creation of the object. This leads to the most efficent code and less error prone code IMO. I suppose that subtlety was lost however I a poorly worded reply on my part. – gbtimmon Jun 05 '13 at 12:31