scope inside module pattern
I want to organize my code better, so I started on a module pattern. But I'm stuck already at the beginning
This is my code (just for purpose of my question I stripped a lot). I want to use the private var array 'product' to push a productId. However in the 'public access methods' part, that var is 'undefined'. So i can not push a value into the array. Does anyone know why or have a solution to this? I would be very happy with it.
- PRODUCTS_MODULE = {};
- PRODUCTS_MODULE.create = (function () {
-
- // private
- var product = []; // define product array
-
- // public access methods
- return {
- // setters
- setProduct: function(productId) {
- this.product.push(productId); // this.product is undefined
- },
-
- getProduct: function() {
- return this.product;
- }
-
- };
- } ());
-
- var productsObj = PRODUCTS_MODULE.create;
-
- productsObj.setProduct(42);
- console.log(productsObj.getProduct());