Building a Cash Register! BLEEP BLEEP


I keep getting an error that it is not the correct total.

Replace this line with your code.


 voidLastTransaction: function() { -= this.lastTransactionAmount;

this.lastTransactionAmount holds 0 not the cost of last item.


What @rcodeman is saying is that the above variable (as written) is a global, not an object attribute.


So what am I supposed to do? I am is only subtracting one and then adding three.



var obj = {
    a: 0,
    f: function () {
        a = 42;

When we run the method and then query obj.a, what will print?







A variable inside a method needs context. After all, it is supposed to be an instance method so should be aware of its instance at time of invokation. The variable this refers to the present context, obj, in this instance.

var obj = {
    b: 0,
    f: function () {
        this.b = 42;
console.log(obj.b));    // 42
console.log(b);         // raises an exception


I forgot: this
when saving the value. Got it, thanks.


You're welcome. I added another example above (and edited the typos (twice)).


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.