0

我有一个如下所示的 JavaScript 对象:

venue = function(map, dataSet) {
    // set some constants
    this.VENUE_ID = 0;
    this.VENUE_NAME = 1;
    this.VENUE_CITY = 2;

    this.filterBy = function(field, value) {
        ...
        var filterValue = 'parent.VENUE_' + field;
    }
}

现在,问题是我需要 的值filterValue来包含父对象上常量的值。目前我已经尝试使用上面显示的方法,然后在尝试访问数组项时引用 filterValue,但这只是返回未定义。

如何将filterValue变量转换为它所代表的常量的值?

4

3 回答 3

3

这与变量范围无关。

var filterValue = this['VENUE_' + field];

会做。

于 2009-07-29T10:26:00.527 回答
2

JavaScript has no concept of 'parent'. And I think you're confusing scope and context. If that method was written as var filterBy() you'd have to access it in a different 'scope'. But by using 'this' you kept in in the same object as it was written. So everything you wrote is in 'this' context.

于 2009-07-29T12:46:26.013 回答
1

试试这个:

var filterValue = this['VENUE_' + field];
于 2009-07-29T10:26:14.533 回答