Saturday, 15 September 2012

javascript - Difference in js object instances -



javascript - Difference in js object instances -

i new kind of stuff, gone through several tutorials of js , found 2 type of coding standards of functions, shown below :

return({ error: function(message){ // code here }, debug: function(message){ // code here } });

and 1 like

this.error = function(message){ //code here }; this.debug = function(message){ //code here }; homecoming this;

i new in coding, have little thought coding formats. can please explain basic difference between above 2 snippets ?, usage of both same .

they pretty much same.

the first returns object literal function:

app.factory('myfactory', function() { var var1 = 'a'; var var2 = 'b'; homecoming { method1: function() { ... }, method2: function() { ... }, property1: var1, property2: var2 }; });

the sec returns function angular phone call this: var fn = new myfunction(); service is.

app.service('myfunction', function() { var var1 = 'a'; var var2 = 'b'; this.method1 = function() { ... }; this.method2 = function() { ... }; this.property1 = var1; this.property2 = var2; });

services create utilize of 'this' keyword because function constructor called (new myfunction()).

both singletons, , both injectable. can see there little difference between two.

javascript

No comments:

Post a Comment