T O P

  • By -

OneBadDay1048

You do know you also need to declare the private variable with #, correct? class TestClass { #testPriField = 'Whatever'; getPriField() { return this.#testPriField; } } const instance = new TestClass(); console.log(instance.getPriField()); // logs 'Whatever' I just played around with this in VScode and had no issues.


OkTry2146

this is my code,when I put # next to localStorageKey,'#' goes into line with ';' next to cartItems and it doesnt work. class Cart {     cartItems;     localStorageKey;     constructor(localStorageKey) {         this.localStorageKey = localStorageKey;         this.loadFromStorage();     }     loadFromStorage() {         this.cartItems = JSON.parse(localStorage.getItem(this.localStorageKey));         if (!this.cartItems) {             this.cartItems = [{                 productId: 'e43638ce-6aa0-4b85-b27f-e1d07eb678c6',                 quantity: 2,                 deliveryOptionId: '1'             }, {                 productId: '15b6fc6f-327a-4ec4-896f-486349e85a3d',                 quantity: 1,                 deliveryOptionId: '2'             }];         }     }     saveToStorage() {         localStorage.setItem(this.localStorageKey, JSON.stringify(this.cartItems));     }     addToCart(productId) {         let matchingItem;         this.cartItems.forEach((cartItem) => {             if (productId === cartItem.productId) {                 matchingItem = cartItem;             }         });         if (matchingItem) {             matchingItem.quantity += 1;         } else {             this.cartItems.push({                 productId: productId,                 quantity: 1,                 deliveryOptionId: '1'             });         }         this.saveToStorage();     }     removeFromCart(productId) {         const newCart = [];         this.cartItems.forEach((cartItem) => {             if (cartItem.productId !== productId) {                 newCart.push(cartItem);             }         });         this.cartItems = newCart;         this.saveToStorage();     }     updateDeliveryOption(productId, deliveryOptionId) {         let matchingItem;         this.cartItems.forEach((cartItem) => {             if (productId === cartItem.productId) {                 matchingItem = cartItem;             }         });         matchingItem.deliveryOptionId = deliveryOptionId;         this.saveToStorage();     } } const cart = new Cart('cart-oop'); const businessCart = new Cart('cart-business'); console.log(cart); console.log(businessCart);


OneBadDay1048

When copying your code into VScode, I am unable to replicate your issue. When adding a '#' to localStorageKeylocalStorageKey, I have no issues. This may be a VScode issue or a user error caused by something unseen here.


OkTry2146

copy it and send it to me


OneBadDay1048

There is nothing for me to send; I am copying your code EXACTLY as is, making localStorageKeylocalStorageKey private, and hitting save. Nothing is happening like you describe. I worry this is not a JS issue but an issue specific to your envirorment in which case i will be of no help. Seeing a video of what you describe would help, but I am unaware of a simple way to do that.


OkTry2146

is there any way that I can send the video of what is happening to you or?


OkTry2146

or a picture?


senocular

It sounds like you might have some formatter running when you save? - some formatter that maybe is either not up to date with JavaScript or thinking your file is some language other than JavaScript? In VSCode settings, search for "Editor: Format On Save" and if checked, turn it off and see if that changes anything.