5

Does anybody know of there's a way to reference the value of a sibling key in a JavaScript object literal?

so use the value of target in the beforeNext() function here:

obj: {
        target: 'li.player a.icon-tag',
        parent: 'ul#drop_list',
        beforeNext: function(){
          target.addClass('bind active');
        }
      }
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
James
  • 431
  • 9
  • 15

2 Answers2

4

This is not a "JSON" object, but a JavaScript Object (or just "Object"). I assume that this is also contained in an Object literal as obj: { by itself is invalid syntax.

Anyway, yes you can reference properties of an object in methods with this.

beforeNext: function () {
    this.target;
}

http://jsfiddle.net/ExplosionPIlls/Q9v8r/

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • @James Don't forget to turn it into a jQuery object, as it's not currently stored as one. – crush Jun 20 '13 at 14:40
  • @crush Thanks guys, the full object is here: `options = { steps: [ { target: 'li.player.playing .share-btns a.icon-tag', parent: 'ul#drop_list', ok:true, beforeNext: function(){ this.target.addClass('blink active'); $("a.icon-tag").bind('click', function() { tour.finish(); }); } } ] };` so `options` is an object with an array `steps` that contains a number of objects. `this.target` doesn't seem to be working, any ideas why? – James Jun 20 '13 at 16:06
  • 1
    @James `target` is just a string; try `$(this.target).addClass` as crush suggested – Explosion Pills Jun 20 '13 at 16:11
0

In case you're dealing with plain JavaScript:

var cartoon = {"george jetson":{"son":"elroy","daughter":"judy"} }

Use the Object constructor to convert a string to an object:

cartoon["george jetson"].son = Object(cartoon["george jetson"].son)

Then bind the sibling value:

cartoon["george jetson"].son.sister = cartoon["george jetson"].daughter

And use toString to get the original value:

cartoon["george jetson"].son.toString()

references

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265