Bleep Bleep: Variable is to Global Variable as This.Key is to...?


#1

Hi all,

So I understand that when we declare a variable without keyword var it automatically becomes a global variable, and that it isn't a good practice because global variables should be created deliberately...

However I would like to know what happens when we refer to a property but don't put that property in the object literal?

I ask because before I did Bleep Bleep I saw a program that omitted this and still seemed to get the right total, and when I got to that chapter I tried doing the same thing and even running it multiple times (Idk, I guess I thought thought maybe lastTransactionAmount might get bigger each time) and seemed to get the expected total...

Why does it "work^" ?

(Also, what is return true; for?)

Thanks everyone :nerd:
frisby


^ Well, at least for me; it looks like the total was a different number for OP.



https://www.codecademy.com/en/courses/close-the-super-makert/0/5?curriculum_id=506324b3a7dffd00020bf661


var cashRegister = {
    total:0,
    //lastTransactionAmount: 0,
    //Dont forget to add your property
    add: function(itemCost) {
        this.total +=  itemCost;
        this.lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    
    voidLastTransaction: function() {
        this.total -= this.lastTransactionAmount;
    }
    
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction();
cashRegister.scan("chocolate", 3);

//Show the total bill
console.log('Your bill is '+cashRegister.total);

Continuing the discussion from 7.Search for a friend:

Continuing the discussion from Getting IN-timate:

Continuing the discussion from Can you help me please , with bleep bleep:


#2

Or not at all, would be the purist's ideal. It called protecting global scope.

We can add properties to an object at any time, on the fly. They don't have to be written in the original object literal expression. But if we attempt to access a property that does not exist, to read it, we will get a reference error.

Let's say your object has a lastTransactionAmount variable (property), and somewhere in the code,

lastTransactionAmount = itemCost;

The variable will be defined on first encounter, in global scope, and continue to accumulate so it will appear to be correct. However, it will not be in the context of the cashRegister object, but the window object.

The intention when the variable was defined in the object was to use THAT variable when referring inside the methods. That is why we must use this.lastTransactionAmount in the add() method and in the voidLastTransaction() method. this is cashRegister.


#3

Thank you for your help, @mtf.

Or not at all, would be the purist's ideal. It called protecting global scope.

In this case, is cashRegister a global variable? What is one way one might go about protecting global scope? The reason I ask is that if I'm understanding correctly, I believe we've declared a lot of global variables over the course of this track.

But if we attempt to access a property that does not exist, to read it, we will get a reference error.

So the reason I could not "see" this working is because the add function was called before voidLastTransaction, i.e. a value existed that void could call upon, but were void called first, without declaring lastTransactionAmount: 0, I would have gotten a Reference Error?

The variable will be defined on first encounter, in global scope, and continue to accumulate so it will appear to be correct. However, it will not be in the context of the cashRegister object, but the window object.

I think I understand this, but what makes the program appear to work

Is it related to your explanations here and when I run voidLastTransactionAmount it looks for this.lastTransactionAmount with this = cashRegister but doesn't find it so it expands to global/window context(scope?) where it finds my unintentionally declared global property this.lastTransactionAmount where this = window/global?