JavaScript - declaring of a function with features -


i'm new js , studied code web apps. can tell me difference between these 2 declarations, both kind of objects featured functions? when use declaration?

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) function can give out information object, 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);   } } 

when this:

(function(){  return {} })(); 

you creating self invoking function is, basically, function created , executed (). ok, function self invoked , return object containing other objects. variable namespace.mycanvas object function returned.

and b function javascript constructor, (when used new keyword) return object xpos, ypos, w , h properties , make method.

hope helps

edit this link can you.. if doubt function declaration , function expression difference


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -