Home:ALL Converter>javascript function returning an undefined object

javascript function returning an undefined object

Ask Time:2016-07-10T22:51:21         Author:pedroyanky

Json Formatter

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

Author:pedroyanky,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38293164/javascript-function-returning-an-undefined-object
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
yy