Link to a function

Refresh

December 2018

Views

57 time

4

I am building a simple framework to work with DOM elements. I have about 40 big functions in the framework.

Framework function is :

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

And it's functions :

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

Here 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;

The 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 and so on..

How to link to functions from Dev.FN without creating them on each $(element) call?

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

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

1 answers

0

Палка ваших заявлений в IIFE :

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

В JavaScript, каждая функция, при вызове создает новый контекст исполнения. ... Во многих случаях вам не нужны будет несколько «экземпляры» любой ваша функция возвращает makeWhatever, и можете сделать с помощью всего одного экземпляра