locked
How to access empty object inside constructor RRS feed

  • Question

  • User1406973109 posted

    Good day all,

    Please I need to access an empty object declared inside a class constructor and create new properties for it.

    I'm getting undefined when I try to access the object inside a method. Please check my code below:

    class ShoppingCart{
      constructor(){
       this.total = 0;
        var items = {}; //Trying to access this object
      }  
      
      addItem(item_name, quantity, price){
        //Object.defineProperty(this.items, item_name, {value:quantity});
        const targetMember = "item_name"; 
        this.items[targetMember.value] = quantity; //This brings undefined
        this.total += price;
      }
      
      removeItem(item_name, quantity, price){
        this.total -= price;
      }
      
      checkout(cash_paid){
        if(cash_paid < this.total)
          {
            return 'Cash paid not enough';
          }
        else
          {
            var balance = this.total - cash_paid;
            return balance;
          }
      }
      
    }
    

    Thanks

    Tim

    Tuesday, June 11, 2019 9:47 PM

Answers

  • User753101303 posted

    Hi,

    What if using just what you have done already for total ie :

    this.items={};

    var is for local variables( as you have done already as well for the balance)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2019 8:18 AM
  • User839733648 posted

    Hi timotech,

    The constructor method is a special method for creating and initializing an object created with a class.

    There can only be one special method with the name "constructor" in a class. 

    When you define a class and use this keyword to set the value, the value is recognized as a public member.

    In that case, you could apply the member out of the constructor.

    Here is a sample.

        <script>
            class Rectangle {
                constructor() {
                    this.height = 9;
                    this.width = 15;
                    this.items = {name:"abc",age:11}; 
                }
            }
            const rec = new Rectangle();
            console.log(rec.height);
            console.log(rec.width);
            console.log(rec.items);
        </script>
    

    If you use var to define a member, it is recognized as a private member in the constructor. So you could not access that out of it.

        <script>
            class Rectangle {
                constructor() {
                    this.height = 9;
                    this.width = 15;
                    var items = {name:"abc",age:11}; 
                }
            }
            const rec = new Rectangle();
            console.log(rec.height);
            console.log(rec.width);
            console.log(rec.items);
        </script>

    For more, you could refer to the MDN about this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2019 8:33 AM

All replies

  • User753101303 posted

    Hi,

    What if using just what you have done already for total ie :

    this.items={};

    var is for local variables( as you have done already as well for the balance)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2019 8:18 AM
  • User839733648 posted

    Hi timotech,

    The constructor method is a special method for creating and initializing an object created with a class.

    There can only be one special method with the name "constructor" in a class. 

    When you define a class and use this keyword to set the value, the value is recognized as a public member.

    In that case, you could apply the member out of the constructor.

    Here is a sample.

        <script>
            class Rectangle {
                constructor() {
                    this.height = 9;
                    this.width = 15;
                    this.items = {name:"abc",age:11}; 
                }
            }
            const rec = new Rectangle();
            console.log(rec.height);
            console.log(rec.width);
            console.log(rec.items);
        </script>
    

    If you use var to define a member, it is recognized as a private member in the constructor. So you could not access that out of it.

        <script>
            class Rectangle {
                constructor() {
                    this.height = 9;
                    this.width = 15;
                    var items = {name:"abc",age:11}; 
                }
            }
            const rec = new Rectangle();
            console.log(rec.height);
            console.log(rec.width);
            console.log(rec.items);
        </script>

    For more, you could refer to the MDN about this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2019 8:33 AM
  • User1406973109 posted

    Thanks all for your help. I don't know what came over me, that I did not realize what I need was just "this"

    Thanks all.

    Tim

    Wednesday, June 12, 2019 12:08 PM