简体   繁体   English

数组在对象文字表示法中添加函数

[英]array adding function in object literal notation

Here is the code I am using, not quite sure how to use literal notation yet, I have to pass the currentActiveCategories to the function somehow. 这是我正在使用的代码,不太确定如何使用文字符号,我必须以某种方式将currentActiveCategories传递给函数。 Not sure if this is even the preferred way to do it, dont want to learn bad habits. 不确定这是否是首选的方法,不想学习坏习惯。

var primaryCare = {
    currentActiveCategories : [ "1", "2"],
    currentValue : addValues(this.currentActiveCategories)
}

function addValues(activeCategories) {
    tempTotal;
    for(int i = 0; i < activeCategories.length; i++){
        tempTotal += activeCategories[i];
    }
    return tempTotal;
}

Currently your object literal creates an object with two properties, currentActiveCategories , which is an array, and currentValue , which is set to the result of calling addValues() at the time the object literal is evaluated. 目前你的对象字面创建具有两个属性,对象currentActiveCategories ,这是一个数组,并且currentValue ,被设定为调用的结果而addValues() 此时的对象常量进行评价。 You are trying to call the function with this.currentActiveCategories , which will be undefined , because this is not equal to that object at that moment. 您正在尝试使用this.currentActiveCategories调用该函数,该函数将是undefined ,因为this它不等于该对象。

If the idea is to have a function that can return the current total at any time you can do this: 如果想要有一个可以随时返回当前总数的函数,你可以这样做:

var primaryCare = {
    currentActiveCategories : [ "1", "2"],
    currentValue : function () {
                      var tempTotal = "";
                      for(var i = 0; i < this.currentActiveCategories.length; i++){
                         tempTotal += this.currentActiveCategories[i];
                      }
                      return tempTotal;
                   }
}

primaryCare.currentValue(); // returns "12", i.e., "1" + "2"

Always declare your variables with var or they'll become globals - note that you can't declare an int in JS. 总是用var声明你的变量或者它们将变成全局变量 - 请注意你不能在JS中声明一个int You need to initialise tempTotal to an empty string before you start adding strings to it, or instead of "12" you'll get "undefined12" . 在开始向其添加字符串之前,需要将tempTotal初始化为空字符串,或者代替"12"您将得到"undefined12"

When you call a function as a method of an object, like primaryCare.currentValue() (as shown above) then within the function this will be set to that object. 当调用一个功能作为一个对象的一个方法,如primaryCare.currentValue()如上所示),则该函数内this将被设置到该对象。

It seems kind of odd to me to be adding the values as strings. 将值添加为字符串对我来说似乎有点奇怪。 If you want to use numbers and get a numeric total you can do this: 如果您想使用数字并获得数字总数,您可以这样做:

var primaryCare = {
    currentActiveCategories : [ 1, 2],   // note no quotes around the numbers
    currentValue : function () {
                      var tempTotal = 0;
                      for(var i = 0; i < this.currentActiveCategories.length; i++){
                         tempTotal += this.currentActiveCategories[i];
                      }
                      return tempTotal;
                   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM