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

  • Home
  • SEARCH
  • 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 8995805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:40:06+00:00 2026-06-15T23:40:06+00:00

If I were to make a new function using the Function constructor, how could

  • 0

If I were to make a new function using the Function constructor, how could I give it a non-temporary scope to access besides window (meaning the scope only has to be evaluated once, not every time the function is called)? The purpose is to construct multiple variables that require some pretty costly calculations, and I don’t want to reconstruct them every time the function is called, but I also don’t want to store them in window. Any ideas?

  • 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-15T23:40:07+00:00Added an answer on June 15, 2026 at 11:40 pm

    For the above described purpose, you use static functions. You cannot prevent scope from being evaluated at every call, because this is the way JavaScript works, but you can speed it up by not having window in the scoping chain.

    var namespace = {};
    namespace.someMethod = function() {
        // do something here.
    };
    

    Now anywhere in your code, you can call that method by using namespace.someMethod();. Just be careful. The above is a static method. You can call it without instantiating. But you MUST NOT use this.property inside a static function. It is a potentially very dangerous operation, as it may give an extension access to the global object and basically un-restricted permissions.

    And the above is a static JavaScript method. It does not have window in the scoping chain.

    Here’s how to create a constructor using the same pattern. When you want to use a constructor, you always instantiate before using. For that you have the new keyword.

    var namespace = {};
    namespace.coordinate = function(x, y) {
        this.x = x;
        this.y = y;
    };
    
    namespace.coordinate.prototype.addCoordinates = function() {
        return this.x + this.y;
    
    
    };
    

    Now anywhere in your code you can do:

    var coordinateObject = new namespace.coordinate(5,10);
    // you have created a new instance.
    alert(coordinateObject.addCoordinates());// will alert 15;
    // now you can make as many as you want. They will behave as instances. 
    // This means they do not interfere with each other in any way.
    // They just have the same properties and methods, but the instance values
    // Can be entirely different.
    
    var secondCoordinateObject = new namespace.coordinate(10, 25);
    alert(secondCoordinateObject.addCoordinates());// will output 35.
    

    You have successufully created an instance of your namespace.coordinate class. Using the pattern I gave you, you can replicate almost the entire functionality of Java or C or any other Object Oriented language.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a function for a new datatype in Standard ML, the
I want to make a constructor function that creates a documentElement object. As an
I'm new to C++ and trying to make two simple functions, but something goes
I want to make new stackpanal from another stackpanal already made before but if
Basically, my client wants to make new visitors enter the site I'm working on
I tried to make a new bare git repository but I get this error:
Every time i make a new JFrame object, the frame appears in the 0,0
I have make a new dropdown navi in css . Chrome, firefox, safari is
I am trying to make a new file format for music. It needs to
why do some people make a new reference in method to a field variable?

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.