Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:返回的对象未定义_Javascript_Oop - Fatal编程技术网

Javascript:返回的对象未定义

Javascript:返回的对象未定义,javascript,oop,Javascript,Oop,我目前正在研究Javascript OOP,在从方法中检索对象作为返回时遇到了一些问题 这是到目前为止我的代码 My Product.js类: class Product { reference; name; description; imgPath; price; constructor(reference, name) { this.reference = reference; this.name = name

我目前正在研究Javascript OOP,在从方法中检索对象作为返回时遇到了一些问题

这是到目前为止我的代码

My Product.js类:

class Product {
    reference;
    name;
    description;
    imgPath;
    price;

    constructor(reference, name) {
        this.reference = reference;
        this.name = name;
    }

    setDescription(description) {
        this.description = description;
        return this;
    }

    setImgPath(imgPath) {
        this.imgPath = imgPath;
        return this;
    }

    setPrice(price) {
        this.price = price;
        return this;
    }
}
class Catalog extends Array {
    addProduct(product) {
        this.push(product);
    }

    getProduct(reference) {
        this.forEach(element => {
            if (element.reference == reference) {
                console.log(element);  // this correctly logs the desired object with the 'G002' reference when called in main.js call 
                return element;
            }
        });
    }
}
My Catalog.js类:

class Product {
    reference;
    name;
    description;
    imgPath;
    price;

    constructor(reference, name) {
        this.reference = reference;
        this.name = name;
    }

    setDescription(description) {
        this.description = description;
        return this;
    }

    setImgPath(imgPath) {
        this.imgPath = imgPath;
        return this;
    }

    setPrice(price) {
        this.price = price;
        return this;
    }
}
class Catalog extends Array {
    addProduct(product) {
        this.push(product);
    }

    getProduct(reference) {
        this.forEach(element => {
            if (element.reference == reference) {
                console.log(element);  // this correctly logs the desired object with the 'G002' reference when called in main.js call 
                return element;
            }
        });
    }
}
My main.js然后创建一个目录并用几篇文章填充它:

let catalog = new Catalog();
let data = [
    ['G001', 'Fender', 'master guitar', 'assets/img/imgf.jpg', 4500],
    ['G002', 'Cort', 'OK guitar', 'assets/img/imgg.jpg', 2000],
    ['G003', 'Squier', 'Cheap guitar', 'assets/img/imgc.jpg', 500]
];

data.forEach(element => {
    let currentProduct = new Product(element[0], element[1]);
    currentProduct.setDescription(element[2]).setImgPath(element[3]).setPrice(element[4]);
    catalog.addProduct(currentProduct);
});

console.log(catalog);  // Correctly logs the object with its 3 Product objs
console.log(catalog.getProduct('G002'));  // Logs undefined !
在my index.html中,这3个脚本按此顺序加载,就在


我不明白为什么main.js中的
catalog.getProduct('G002')
方法调用将返回undefined,因为foreach循环中的元素在catalog.js中的引用匹配时被正确记录。 我的猜测是,一旦执行超出foreach范围,对象就会被销毁?
如果我试图做的事情是可能的,那么我如何从这个方法调用中得到一个返回的对象呢?

这个
getProduct(reference)
方法本身不返回任何东西。在对
forEach
的回调中,您有一个
return
。看见您可以改为使用
getProduct(reference){returnthis.find(element=>element.reference==reference);}
或者使用常规循环。因此,我重写了getProduct():
getProduct(reference){for(让这个的元素){if(element.reference==reference){console.log(element);return element;}}
现在它可以工作了!感谢您指出这是foreach中的回调:在阅读了您的链接文章之后,现在变得更有意义了。。。现在来找出如何将答案标记为解决方案(以及糟糕的编辑…)@平阳不能将其标记为答案,因为它是一条注释。
getProduct(reference)
方法本身不返回任何内容。在对
forEach
的回调中,您有一个
return
。看见您可以改为使用
getProduct(reference){returnthis.find(element=>element.reference==reference);}
或者使用常规循环。因此,我重写了getProduct():
getProduct(reference){for(让这个的元素){if(element.reference==reference){console.log(element);return element;}}
现在它可以工作了!感谢您指出这是foreach中的回调:在阅读了您的链接文章之后,现在变得更有意义了。。。现在来找出如何将答案标记为解决方案(以及糟糕的编辑…)@平阳你不能把它作为一个答案,因为它是一个评论。