0

I want to create an object with class Article and instance method createArticle in javascript.

Nothing appear in my console, even the error message.

Ty for your help !

class Article {
    constructor (nameArticle, price, image, description, type){
        this.nameArticle = nameArticle;
        this.price = price;
        this.image = image;
        this.description = description;
        this.type = type;
    }
    createArticle (){
        if (nameCreateArticle.value && priceCreateArticle.value && urlCreateArticle.value && descriptionCreateArticle.value && radioCreateArticle.value){
            nameArticle = nameCreateArticle.value;
            price = priceCreateArticle.value;
            image = urlCreateArticle.value;
            description = descriptionCreateArticle.value;
            type = radioCreateArticle.value;
            let newArticle = new Article (nameArticle, price, image, description, type);
            popUp.style.display = 'inline';
            popUpMessage.innerText = 'Article has been created.';
            console.log ('toto')
            console.log (newArticle);
        } else if (nameCreateArticle.value === '' || priceCreateArticle.value === '0'){
            console.log ('Error');
        }
    }
    deleteArticle(){
        
    }
}
formCreateArticle.addEventListener ('submit', (e) => {
    e.preventDefault();
    newArticle.createArticle();
})```
This is what i tried and my console still empty, no 'toto' message and no 'Error' message is here. I thought i forget preventDefault but i didnt so i dont understand what's going wrong
Elias P
  • 3
  • 3
  • Can you please complete the example with the instance creation and an example of how you're actually calling `createArticle` method? – Teemu Dec 14 '22 at 07:55
  • Can you provide us with more details about how you're calling the function `createArticle`? – h.howard Dec 14 '22 at 07:55
  • i changed my code and i call createArticle when i submit my form now – Elias P Dec 14 '22 at 08:06
  • You haven't created an instance of your class. You need to do it like `const article = new Article(...); article.createArticle();`. I'd also considered using a ton of external variables in a class as a bad practice, read these values into the properties of the instance instead. – Teemu Dec 14 '22 at 08:14
  • i create an article in my instance : let newArticle = new Article (nameArticle, price, image, description, type); – Elias P Dec 14 '22 at 08:18
  • OK, if that's the case, then the conditions in the `if`s are never passed. Add an extra unconditioned log at the beginning/end of the method, this way you can check the method really is called. At step two, add a break to the `if`s, and run the code. When the execution is halted, check the values of the properties in the conditions to see why a condition is not passed. – Teemu Dec 14 '22 at 08:24
  • My actual code give me this error in the console ONLY when i submit my form. here is the error `script.js:49 Uncaught ReferenceError: newArticle is not defined at HTMLFormElement. (script.js:49:5)` – Elias P Dec 14 '22 at 08:43
  • Then you've created the instance out of the scope of the event listener (or it's not yet created at the time of the submission). See https://stackoverflow.com/a/500459/1169519 – Teemu Dec 14 '22 at 08:46
  • The class design is broken. An `Article` instance never ever should feature [prototypal methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#prototype_methods) like `createArticle` and/or `deleteArticle`. Such methods rather should be [static members](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#static_methods_and_properties) of the `Article` namespace. – Peter Seliger Dec 14 '22 at 11:09

0 Answers0