Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:01:22+00:00 2026-06-03T14:01:22+00:00

We have some js code splitted in many files. We have a core file

  • 0

We have some js code splitted in many files. We have a core file that defines code used by many other js files.

Currently we have something like this:

core.js:

window.mycore = function() {

    var myfunction1 = function() {
    };
    var myfunction2 = function() {
    };
    var myfunction3 = function() {
        //..
        var a =  myfunction1(b);
        //..        
    };
    //...
    // many "myfunction"
    //...
    var myfunctionN = function() {
    };
    var publish = function() {
        for(var i = 0; i < arguments.length; i++) {
            try {
                window.mycore[arguments[i]] = eval('(' + arguments[i] + ')');
            }
            catch(e) {
                Log.err(600, arguments[i], e);
            }
        }
    };
    publish("myfunction1", "myfunction7", "myfunction8",/*...*/"myfunctionM")
}

app.js:

// ...
// ...
var result = window.core.myfunction1("myparam");
// ...
// ...

Note that none core methods are declared as members of the window.core object. Instead they are attached to the core object with the publish function.

This has some pros:

  • The core code can reference any core function without the need of writing “window.core.”
  • We avoid writing “var myfunction = window.mycore.myfunction = function() …” in every public function declaration
  • The exposed methods can be seen centraliced.

But, the use of eval in the publish function is bringing us problems when using code analysis tools since they don’t tend to understand eval declarations.

So, here is my question.
Which is the better way to improve this code, so we can keep the advantages mentioned but eradicating the eval declaration.
I am aware of the solution of sending to the publish function some name/value pairs like publish({‘myfunction1’: myfunction1}, … ), but I also want to avoid function name repetitions.
Consider that I am not looking for radical changes since there is a lot of code written already.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:01:25+00:00Added an answer on June 3, 2026 at 2:01 pm

    I’m not sure I understand completely your reasons for using the “publish” method, but is there any reason your not just returning an object with the correct functions from your constructor?

    ie:

    window.mycore = (function() {
       var myFunc1 = function(a) {
          alert(a);
       };
    
       var myFunc2 = function(b) {
          // call to other function in the same scope
          myFunc1(b);
       }
    
       ...
    
       // at the end just expose the public members you want
       return {
          myFunc1: myFunc1,
          myFunc2: myFunc2
       };
    })();
    

    or

    window.mycore = (function() {
       return {
          myFunc1: function(a) {
             alert(a);
          },
          myFunc2: function(b) {
             this.myFunc1(b);
          }
       };
    })();
    

    or, yet another way to end up with the same object 🙂 … as always there are different ways to get there

    (function(){
    
        var o = {};
    
        o.func1 = function(a) {
            alert(a);
        }
    
        o.func2 = function(b) {
            this.func1(b);
        }
    
        window.mycore = o;
    
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code in Netbeans 6.1 editor that looks like this fooString(8) fooString(8)
I have some code that shows a tables row when something is clicked. So,
I have some code i'd like to refactor that uses a C# iterator (ie
I have some code that parses an xml file using XDocument . It retrieves
I have some code in a javascript file that needs to send queries back
I have some code that's currently giving me an error, because recur can only
I have some code using a System.Transactions.TransactionScope , that creating a new instance of
I have some code like this: doDatabaseFetch { ... @synchronized(self) { ... } }
I have some code that sets up a dictionary with some defualt values for
I have some code using Lucene that leaves the default conjunction operator as OR,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.