具有公共属性的变量数组

array of variables with common properties

本文关键字:变量 数组 属性      更新时间:2024-03-23

我创建了一种滑块,它基于一个名为workChart的变量,该变量包含从1到15的值(可能更多)。现在这些元素(作品)也被分为三类。我还定义了全局列表,其中包含每个类别的作品名称:

gtitlesCol = [12,7,6,3,2,1];
gtitlesRech = [13,5,4];
gtitlesCorp = [15,14];

现在我希望网站上的每一个价值(作品)都会被记录下来,成为其类别的一部分,这样我就可以避免这样的事情:

switch( workChart ) {
    case 3:  switchCategory( "Recherche");   break;
    case 5:  switchCategory( "Collection" ); break;
    case 12: switchCategory( "Recherche");   break;
    case 13: switchCategory( "Corporate" );
};

有没有一种聪明的方法可以将类别"嵌套"到workChart值中?这样我就可以使用变量workChart,而不再需要每次都告诉函数它属于哪个类别。我认为这与将它们创建为具有属性(类别f.e)的对象(工作)有关,但我不知道如何处理它。有什么帮助吗?非常感谢!

@Tobias

对不起,托比亚斯,我想我的问题比你想象的还要简单,我糟糕的解释让你浪费了时间。抱歉。我成功地用做了我想做的事

var categories = { 1:"Collection", 2:"Collection", 3:"Collection", 4:"Recherche", 5:"Recherche", 6:"Collection", 7:"Collection", 8:"Collection", 9:"Collection", 10:"Collection", 11:"Collection", 12:"Collection", 13:"Recherche", 14:"Corporate", 14:"Corporate" }

然后我可以做

alert( categories[5] );

或者用它做一些其他的事情。现在,我想,唯一缺少的是,为我的数组提供一种更令人愉快的语法形式,这样就不会重复15次,而不是我想的3次。

是的,您可以将WorkChart创建为对象。下面你会发现一个示例实现,假设你可能想用这个类别和值做一些事情(以任何方式处理),然后准备好返回:

function WorkChart(cat,objVal) {
    var category = cat,
        retValue = objVal,
        getCategory = function() {
            // any code handling category
            return category;
        },
        retValue = function() {
            // any code handling value
            return retValue;
        };
        return {
            category: getCategory,
            value: retValue
        }
};

var wrkCh1 = new WorkChart("Recherche",13);
var category = wrkCh1.category();
var value = wrkCh1.value();

这是一个简单的对象,只用于返回数据。如果你想在创建后有机会更改对象值,你可以将上面的更改为setters/getters版本,比如:

function WorkChart(cat,objVal) {
    var category = cat,
        retValue = objVal,
        getCategory = function() {
            return category;
        },
        getValue = function() {
            return retValue;
        },
        changeCategory = function(newCat) {
            // any code handling category
            category = newCat;
        },
        changeValue = function(newVal) {
            // any code handling value
            retValue = newVal;
        };
        return {
            category: getCategory,
            value: getValue,
            setCategory: changeCategory,
            setValue: changeValue
        }
};
var workCh1 = new WorkChart("Recherche",13);
var category = workCh1.category();
var value = workCh1.value();
workCh1.setCategory("Corporate");
workCh1.setValue(15);
category = workCh1.category();
value = workCh1.value();

关于你的评论,有很多方法可以创建一个对象数组,然后将其中一个传递给函数,例如

var workCharts = new Array();
workCharts.push(new WorkChart("category1",1));
workCharts.push(new WorkChart("category1",3));
workCharts.push(new WorkChart("category1",5));
workCharts.push(new WorkChart("category2",2));
workCharts.push(new WorkChart("category2",4));
//... and so on until you add them all
// now let's take any element
var workChart = workCharts[1];
doSomethingWithWorkChart( workChart);
doSomethingWithWorkChartCategory (workChart);

好吧,在看到你的编辑后,我可以告诉你,你可以通过一个开关功能来实现这一点,并保留你的3个数组gtitlesCol、gtitlesRich和gtitlesCorp。

function switchCatgegoryByNumber (workChart) {
    if(gtitlesCol.indexOf(workChart != -1) {
       switchCategory( "Collection" );
    }
    else if (gtitlesRech.indexOf(workChart != -1) {
       switchCategory( "Recherche" );
    }
    else if (gtitlesCorp.indexOf(workChart != -1) {
       switchCategory( "Corporate" );
    }
}