I am working on trying to make an JS object and access to private methods. The problem I am running into when trying to return a function is the private methods cannot be ac
Fixed your code here: http://jsfiddle.net/pratik136/JryAk/
Items changed:
return
statementItem
is a var, you were trying to instantiate a class object item2
I don't think that pattern will work for what you're trying to do. I think using a pattern like this one will keep your code smaller and more reusable. This way you also get rid of the set
functions.
var Item = function(options) {
var opts = $.extend({
price: 0,
name: '',
description: '',
quantity: '',
attributes: {}
}, options);
// ...
this.getPrice = function() {
return opts.price;
};
// ...
};
var item = new Item({
price: 100,
name: 'onehundred',
// ...
});
alert(item.getPrice());