i saw this code online and this is its contents.
var shoppingcart = (function () {
var _calculatePrice = function() {
return this.price * this.amount;
};
return {
calculatePrice : _calculatePrice
}
})();
var goods = {
name: 'hammer',
price: 199,
amount: 2
}
shoppingCart.calculatePrice.call(goods)
And this is where i am not comfortable. the function returning an undefined object in {calculatePrice : _calculatePrice}. how does that really work? i thought it will do no harm to return just _calculatePrice
Iceman :
Firstly Typo at line 1.: change shoppingcart to shoppingCart \n\n\r\n\r\nvar shoppingCart = (function() {\r\n\r\n var _calculatePrice = function() {\r\n return this.price * this.amount;\r\n };\r\n return {\r\n calculatePrice: _calculatePrice\r\n }\r\n})();\r\n\r\nvar goods = {\r\n name: 'hammer',\r\n price: 199,\r\n amount: 2\r\n}\r\nconsole.log(shoppingCart.calculatePrice.call(goods))",
2016-07-10T14:56:03