0

I'm going through a course and working through the example on object literal expressions, the example code given

'use strict';
var price = 5.99, quantity = 10;
var prod = { price: 7, quantity: 1, 
    calc() {
        return this.price * this.quantity
    }
};
console.log(prod.calc());

says that when run, the console should have 59.90 in it as "this" is referring to the context of the code, not the object that contains the function. When I run the code (using the latest Chrome) the console shows 7. If I remove the two "this."

return price * quantity

it returns 59.90 which is the opposite of what the video says it should be doing. Any idea where I'm going wrong with this? Thanks

Glyn
  • 282
  • 1
  • 18
  • 2
    Either the video is wrong, or you misunderstood it. We can't solve that here. – Denys Séguret Jan 13 '17 at 09:32
  • 2
    *"says that when run, the console should have 59.90 in it as "this" is referring to the context of the code, not the object that contains the function."* That's incorrect. *"Any idea where I'm going wrong with this?"* You aren't, if that's really what the video said.. I suggest finding a different resource, as apparently the one you're using isn't as reliable as one would like. :-) – T.J. Crowder Jan 13 '17 at 09:34
  • Note that this type of literal is a very recent feature of JavaScript. It's possible that a tutorial was badly updated. – Denys Séguret Jan 13 '17 at 09:36
  • I voted for closing as off-topic, because we can't solve that here. I don't find this linked QA useful. – Denys Séguret Jan 13 '17 at 09:37
  • It's plural sight which have a good reputation (especially as work are paying for it :) – Glyn Jan 13 '17 at 09:38

0 Answers0