开发者

How to access content in an object literal?

开发者 https://www.devze.com 2023-03-27 15:07 出处:网络
Can someone tell me wha开发者_如何学运维t the best way is to store content in an object literal and how to access it using my JS pattern? I can\'t seem to get it to work.

Can someone tell me wha开发者_如何学运维t the best way is to store content in an object literal and how to access it using my JS pattern? I can't seem to get it to work.

Namespace.object = {
    var data = [{"myid1" : "<p>My content 1</p>"}];

    method: function () {
        var myData = Namespace.object.data[0].id;
    }
};


To store:

Namespace.object = {
    data: [{"myid1" : "<p>My content 1</p>"}],

    method: function () {
        var myData = Namespace.object.data[0].id;
    }
};

To access:

var theFirstData = Namespace.object.data[0];

var myId1 = Namespace.object.data[0].myid1;

// Alternatively...
var myId1b = Namespace.object.data[0]['myid1'];

Namespace.object.method();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号