Getting sibling value of key in a JavaScript objec

2019-07-03 00:15发布

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');
        }
      }

2条回答
男人必须洒脱
2楼-- · 2019-07-03 00:32

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/

查看更多
做个烂人
3楼-- · 2019-07-03 00:35

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

查看更多
登录 后发表回答