I'm new to JS and studied some code from web apps. Can anybody tell me the difference between these two declarations, are they both kind of objects with featured functions? When use which declaration?
a) a self evoking function:
namespace.myCanvas= (function(){
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var foo = function(...){...}
return {
canvas: canvas,
context: context,
foo: foo
}
})();
b) a function that can give out information about an object, I guess:
function makeRectangle(xPos, yPos, w, h) {
this.xPos= xPos;
this.yPos= yPos;
this.w= w;
this.h= h;
this.make= function() {
ctx.fillStyle = this.fill;
ctx.fillRect(this.xPos, this.yPos, this.w, this.h);
}
}