You do not assign to the _instanceobject, it is just a closing variable, and it should be accessed without using this:
var Dog = function() {
var _instance = 'hello world';
return function() {
console.log(_instance);
}
} ();
var l = new Dog();
I would probably write it like this:
var Dog = (function() {
var defaults = {
name: 'Rags'
};
var Dog = function (options) {
this.setOptions(options);
};
Dog.prototype = {
setOptions: function (options) {
var key = null;
for ( key in defaults) {
this[key] = defaults[key];
}
if (options && options.hasOwnProperty) {
for ( key in options ) {
this[key] = options[key];
}
}
}
};
return Dog;
} ());
var buster = new Dog({name: 'Buster'}),
unnamed = new Dog();
alert(buster.name);
alert(unnamed.name);
Please note that I did not try to compile the above code, so it may contain several errors. JsLint can't handle anything !
, setOptions, , , .., -.
, JQuery , () , setOptions:
function setOptions (options) {
var mergedOptions = $.extend(true, defaults, options);
for (var key in mergedOptions ) {
if(this.checkAllowedProperty(key, typeof(mergedOptions[key])) {
this[key] = mergedOptions[key];
}
}
}
function checkAllowedProperty (propertyName, dataType);