Function reference

I am creating a simple structure for working with DOM elements. I have about 40 great features in the framework.

Framework Function:

var $ = function(a) { /* content */ }; 

And it functions:

 Dev.FN = { /* big functions here */ }; 

This is how I include functions in the framework:

  if($.FN) { $.FN.em = em; return $.FN; } $.FN = { em : em }; for(var b in Dev.FN) { if(Dev.FN.hasOwnProperty(b)) { $.FN[b] = Dev.FN[b]; } } return $.FN; 

Problem: It creates my 40 big functions for each framework call , I mean: if I call $(element1) , it creates 40 functions for it, then $(element2) again 40 big functions, etc.

How to bind functions from Dev.FN without creating them each time $(element) called?

PS: I also tried the following, but I don't know if this helps:

  for(var b in Dev.FN) { if(Dev.FN.hasOwnProperty(b)) { $.FN[b] = function() { return Dev.FN[b].apply(this, arguments); }; } } 
+4
source share
1 answer

Insert your ads in IIFE :

 Dev.FN = (function () { /* big functions here */ }) (); 

In JavaScript, every function creates a new execution context when called .... In many cases, you will not need several "instances" of what your makeWhatever function returns, and can do with only one instance

0
source

All Articles