1) Comparision
2) Fake instance for literal object
3) Function contruct with literal wihout ctxt methods, inside are private method with return what is public
4) Function construct in closure (better performance due to the division of methods and the remo
// define the objects:
var objLit = {
x: 0,
y: 0,
z: 0,
add: function () {
return this.x + this.y + this.z;
}
};
var ObjCon = function(_x, _y, _z) {
var x = _x; // private
var y = _y; // private
this.z = _z; // public
this.add = function () {
return x + y + this.z; // note x, y doesn't need this.
};
};
// use the objects:
objLit.x = 3;
objLit.y = 2;
objLit.z = 1;
console.log(objLit.add());
var objConIntance = new ObjCon(5,4,3); // instantiate an objCon
console.log(objConIntance.add());
console.log((new ObjCon(7,8,9)).add()); // another instance of objCon
console.log(objConIntance.add()); // same result, not affected by previous line
var myExplorer = function() {
var options;
var init = function(settings) {
var config = {
$wrapper : $('#explorerCategories'),
$contentHolder : $j('#categoryContent'),
loadingImg : '<img src="../images/standard/misc/ajax_loader.gif" alt="loading" class="loading" />'
}
// provide for custom configuration via init()
options = $.extend(config, settings);
// some more code...
};
var createExpanderLink = function() {
// more code
};
var anotherMethod = function() {
// etc
};
// Public API
// return the functions you want to use outside of the current instance
return {
init : init,
createExpanderLink : createExpanderLink,
anotherMethod : anotherMethod
}
}
var firstExplorer = new myExplorer();
var secondExplorer = new myExplorer();
// etc
var Test = (function () {
function _insert() {
console.log(this);
$("#a").html(this.text);
}
function Test() {
this.text = 'Pokusný text';
}
Test.prototype.run = function () {
_insert.call(this);
};
return Test;
})();
$(document).ready(function(){
var x = new Test();
x.run();
});