简体   繁体   English

如何扩展 javascript 数组 Class?

[英]How to extend the javascript Array Class?

I am trying to extend the Array class to add a Sum method to it.我正在尝试扩展数组 class 以向其添加 Sum 方法。 This is my code below, what am I doing wrong?这是我下面的代码,我做错了什么?

Class tipArray extends Array{
    sum(values) {
        for(i in values) {
            total +=i;
        }
    }
}

var testArray = new tipArray();
testArray = [1,2,3,4];
console.log(testArray.sum());

Expected output = 10预期 output = 10

  1. Start by imagining how you would sum an array (maybe something with reduce ). 首先想象一下如何对一个数组求和(也许带有reduce )。
  2. Turn that into a function. 把它变成一个函数。
  3. Add that as a method on your class. 将其添加为类中的方法。 You can use this to refer to the array. 您可以使用this来引用数组。
  4. (optional) ask yourself if you really need a subclass instead of a function that accepts an array. (可选)问自己是否真的需要一个子类而不是一个接受数组的函数。

 class tipArray extends Array{ sum() { // Consider making sure the array contains items where sum makes sense here. return this.reduce((sum, current) => sum + current) } } var testArray = new tipArray(1,2,3,4); console.log(testArray.sum()); // add another element and take another sum testArray.push(10) console.log(testArray.sum()); 

class tipArray extends Array {
    sum() {
        let val = 0;

        for (let i = 0; i < this.length; i++) {
            val += this[i];
        }

        return val;
    }
}

var testArray = new tipArray(1, 2, 3, 4);
console.log(testArray.sum());
console.log(testArray.length);

Inside of the sum method, you refer to the array via this . sum方法内部,您可以通过this引用数组。

I'm sorry, but I had to downvote the question as it seems to me more like asking for the solution of a homework after doing half the research on how to code in javascript.对不起,但我不得不否决这个问题,因为在我看来,在对如何在 javascript 中进行编码进行了一半研究之后,我更像是在寻求家庭作业的解决方案。

  1. keywords, like class in javascript needs to be lowercase.关键字,例如class中的 class 需要小写。
  2. you didn't declare any of the variables.你没有声明任何变量。 For example in the for loop you keep adding i to total , but what was the initial value of total ?例如,在 for 循环中,您不断将i添加到total中,但是total的初始值是多少? Declare the total variable before the for loop as let total = 0在 for 循环之前声明total变量为let total = 0
  3. the loop index i also needs to be declared as for (let i in values) {循环索引i也需要声明为for (let i in values) {
  4. the sum function doesn't return anything as you don't have any return statements.总和 function 不会返回任何内容,因为您没有任何返回语句。 In this case javascript returns undefined and that is what you see in your console log.在这种情况下 javascript 返回未定义,这就是您在控制台日志中看到的内容。 Add return total after your for loop so that the function would return something.在 for 循环之后添加return total ,这样 function 就会返回一些东西。
  5. no need to use reduce here or extending array prototype as the issue isn't because for loop not being suitable to calculate sums.不需要在这里使用 reduce 或扩展数组原型,因为问题不是因为 for 循环不适合计算总和。
  6. you create a new instance of your own array class only to override it with a simple array at the next line with testArray = [1,2,3,4] , as that line is the same as testArray = new Array(1, 2, 3, 4);您创建自己的数组 class 的新实例只是为了在下一行用简单数组覆盖它testArray = [1,2,3,4] ,因为该行与testArray = new Array(1, 2, 3, 4); . . Write either var testArray = new tipArray(1, 2, 3, 4) or testArray.push(1, 2, 3, 4) .编写var testArray = new tipArray(1, 2, 3, 4)testArray.push(1, 2, 3, 4)

In essence the solution should be what Geuis wrote.本质上,解决方案应该是 Geuis 所写的。

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

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