What is the standard javascript convention for a web page (code will not be used anywhere else). I ask about this because I am always confused about what technique should be followed? And if we say that I need to write a generic lib code / utility, which one is the best technique? Let me know if there are other ways? I share some methods that I know of. What is the difference between each and when should you use the technique below?
example 1:
((function (mine) {
mine.foo = 'this is a property';
mine.bar = function(){
return 'this is a function with ' + mine.foo;
};
})(window.mine = window.mine || {});
alert(mine.bar());
Example 2:
var mine = function(){};
mine.foo = 'this is a property';
mine.bar = function(){
return 'this is a function with ' + mine.foo;
}
alert(mine.bar());
Example 3:
var mine = {
'foo' : ' this is a property ',
'bar' : function(){
return ' this is a function with '+ mine.foo;
}
};
alert(mine.bar());
example 4:
function Mine() {
this.foo = 'this is a property';
}
Mine.prototype.bar = function(var1){
return ' this is a function with '+ this.foo;
};
var mine = new Mine();
alert(mine.bar());
Example 5:
var foo = 'this is a property';
function bar(){
return ' this is a function with '+ foo;
}
alert(bar());
thank
EDIT : JavaScript-Module-Pattern is IMO path
surya source
share