How AI is applied across API Evangelist and APIs.io. Read my AI disclosure →
API Evangelist API Evangelist
Discovery
Learnings
Guidance
Toolbox
Alignment
API Evangelist LLC

Keep Functions Tidy

calendar_today May 17, 2013 person domain json

When you want to keep your functions out of the global namespace, use JSON. A common development pattern for this is placing functions inside an object looks like this: var nameSpace = { “myFunc” : function ( x , y ){ return x + y ; }, “otherFunc” : function ( x , y , z ){ return this . myFunc ( x , y ) * z ; } } Now you can access your functions the same way you access any other object property. In this case, the functions are referred to as “methods”. console . log ( nameSpace . myFunc ( 1 , 2 ) ); // returns 3 Another advantage you have of declaring functions in an object literal is gaining access to the this variable within each method. In the example above, this refers to nameSpace object. It can be referred to easily in this manner without declaring any functions in the global namespace. Handy indeed.

open_in_new Read original post