微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JS:无法读取未定义的属性“ sort”-定义类函数的参数

如何解决JS:无法读取未定义的属性“ sort”-定义类函数的参数

我正在完成我的Twilioquest Javascript教程,最后的任务之一是用一些函数构建一个类。每次我运行这个我都会 TypeError:无法读取未定义的属性“ sort”。 (位于array.sort();

class Ducktypium {
    constructor(color) {
        this.color = color;
        if (color != 'red' || 'blue' || 'yellow' ){
            throw error = ("Wrong color!");
        }
    }

    calibrationSequence = [];

    calibrate(input) {
        let array = input;
        let calibrationSorted = array.sort();
        let calibrationMuliplied = calibrationSorted.forEach(item => {
            item * 3;
        });
        return calibrationMuliplied
    }
    calibrationSequence = this.calibrate();
}

var dt = new Ducktypium('red');
dt.calibrate([3,5,1]);
console.log(dt.calibrationSequence); // prints [3,9,15]

错误发生在该类的calibrate()函数上。可能有更多的错误,但是我将重点放在控制台向我抛出的错误上。 我检查了类似的问题,并认为我理解了这个问题,但是它们的背景不同,我无法解决

据我了解,array没有收到我试图通过dt.calibrate([3,15]);传递的值,因此.sort()函数不适用于任何东西。但是代码看起来与其他尝试执行类似操作的尝试类似,所以我只是找不到问题。

解决方法

这是正确的代码,带有详细的说明

class Ducktypium {
    constructor(color) {
        this.color = color;
         // && means AND while || means OR
         // You have to be specific with your conditions and the if statement
         // only runs when the condition provided is met
        if (color != 'red' && color != 'blue' && color != 'yellow' ) {
            throw Error("Wrong color!");  // You throw an Error object
        }
    }

    // calibrationSequence = [];

    calibrate(input) {
        let array = input;
        let calibrationSorted = array.sort();
        // This line is wrong. forEach loops through the items in your array and pass that item into a function but doesn't return anything
        // let calibrationMuliplied = calibrationSorted.forEach(item => {
        //     item * 3;
        // });

        // What you intended was the map() function
        // map() calls the function you provide on each item and modifies them in the array
        let calibrationMultiplied = calibrationSorted.map((item) => {
            return item * 3
        });

        // The difference between forEach and map is that
        // Lets say you have a box of numbered balls
        // in forEach you take out each ball and note the number multiplied by two and put the
        // ball back
        // in map you take out each ball and actually rewrite the number as its number multiplied
        // by two and put it back in
        
        return calibrationMultiplied
    }
    // You are calling the calibrate method without passing anything into "input"
    // that's why sort() is called on nothing and you get an error
    // calibrationSequence = this.calibrate();
}
,

您对“ calibrationSequence = this.calibrate()”的陈述有误,请使其像下面的代码以及“ if (color != 'red' || 'blue' || 'yellow' )”这样,且条件为(this.color !== 'red' && this.color !== 'blue' && this.color !== 'yellow' )

calibrationSequence = [];

calibrate(input) {
  let array = input;
  let calibrationSorted = array.sort();
  let calibrationMuliplied = calibrationSorted.forEach(item => {
    item * 3;
  });
  return calibrationMuliplied
}
calibrationSequence = this.calibrate;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。