JavaScript:嵌套对象值可以引用其父对象键吗?

时间:2016-10-23 15:44:12

标签: javascript object nested parent

我有一个如下对象:

var obj = {
    parentKey : {
        nestedKey1 : value, 
        nestedKey2 : function () {
            return parentKey + nestedKey2;
        }
    }
};

有没有办法在其中一个嵌套键值对中保存的函数中使用parentKeynestedKey1和/或nestedKey2的实际名称中的值上面的一个?

在我的实际情况中,keys是我希望用作for loop条件的所有数值。

更新了要提问的方案

作为学习JavaScript的练习,我决定编写电梯系统的逻辑。为了做到这一点,我已经制作了一个代表建筑物的物体,这个物体包含每个楼层,每层楼内的居民数据是希望前往另一层楼的人数如下:

var floorData = {
    <floor number> : {
        <people going to floor X> : <number of people>,
        <people going to floor Y> : <number of people>,
        peopleGoingUp : <formula that sums amount of people going up>,
        peopleGoingDown : <formula that sums amount of people going down>
    }
};

我希望在每个<floor number>对象中包含一个属性,该属性通过公式对peopleGoingUppeopleGoingDown的数量进行求和。要使此公式符合我的要求,我需要访问<floor number>名称,该名称是peopleGoingUppeopleGoingDown公式中的数值。

到目前为止,这是我的工作示例,我在peopleGoingUp楼层中添加了peopleGoingDowntheBuilding[2]个公式。我希望将theParentKey的手输入值更改为等于父对象的名称。

// Total number of floors in building global object
var totalFloors = 3;

// Building object including amount of people waiting to take lift to other floors on each level of the building
var theBuilding = {
    0 : {
        1 : 2,
        2 : 0,
        3 : 1,
    },
    1 : {
        0 : 1,
        2 : 3,
        3 : 2,
    },
    2: {
        0 : 2,
        1 : 1,
        3 : 4,
        goingUp : function () {
            var sum = 0;
            var theParentKey = 2; // this is supposed to be a direct reference to the parent object name to this nested object and thus equal to 2
            for (i = 0; i <= totalFloors; i++) { // loop for total floors 
                if (i !== theParentKey && i >= theParentKey) {//sum if i isn't = this floor number and that i is greater than this floor number
                    sum += this[i]
                }
            };
            return sum;
        },
        goingDown : function () {
            var sum = 0;
            var theParentKey = 2; // this is supposed to be a direct reference to the parent object name to this nested object and thus equal to 2
            for (i = 0; i <= totalFloors; i++) { // loop for total floors from lowest
                if (i !== theParentKey && i <= theParentKey) { //sum if i isn't = this floor number and that i is less than this floor number
                    sum += this[i]
                }
            };
            return sum;
    },
    3 : {
        0 : 0,
        1 : 1,
        2 : 4
    }
};

console.log(theBuilding[2].goingUp()); // 4
console.log(theBuilding[2].goingDown()); // 3

3 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你想得到钥匙的名字。您可以在传递当前对象df.to_sql('INDEX_BASIC_TABLE', engine, if_exists='replace', index=False) 时使用Object.keys()

this

答案 1 :(得分:1)

看到这是一个例子

&#13;
&#13;
    var trHTML = '';

    $.each(data.Titles, function (i, item) {
        var v = 0;


        trHTML += '<tr>';

        if(v <= 2){
            trHTML += '<td><a href="' + data.Links[i] + '">' + data.Titles[i] + '</a><br><img src="' + data.Images[i] + '"></td>';
        }
        else{
            var v = 0;
            trHTML += '</tr>';
            trHTML += '<tr>';
        }

        trHTML += '</tr>';
        v++
    });

    $('#location').append(trHTML);

    },
&#13;
&#13;
&#13;

答案 2 :(得分:1)

  

有没有办法在实际名称中使用这些值   parentKey和nestedKey1和/或nestedKey2在一个函数中保存   嵌套键值对的结果如上所述?

你可以用两种方式做到这一点。

使用词汇范围

nestedKey2内的函数可以访问全局范围。因此,您可以使用obj

覆盖.内的每个媒体资源
obj.Parentkey

使用this

这有点棘手,因为多级对象中的函数内的this将指向&#34;相同的级别&#34;函数所在的对象的位置。您可以将this.一起使用来到达对象的较低级别,但无法达到较高级别。

但是,您可以使用实施circular reference的解决方法:

var myObj = {

    levelAccess: function() {
        this.two.__ = this;
        return this;
        },

    one: 'one',
    two: 
        {
        __: [],

        myFunction: function () {return this.__.one}
        }

    }.levelAccess();

此解决方案要求您为需要访问更高级别的每个级别添加__属性,然后通过__函数初始化所有levelAccess属性。解决方案不应导致任何内存泄漏,请参阅this