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 7671383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:02:30+00:00 2026-05-31T16:02:30+00:00

It is common and easy to create function like: var f = function(){ alert(something);

  • 0

It is common and easy to create function like:

var f = function(){
    alert("something");
};

So why is there Function object like:

var f = new Function("alert('something');");

The latter is hard to write/read. I can only come up with one situation in which someone typing some JS code to the webpage and running it. This can also be solved with eval.

Why Function object?

  • 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-05-31T16:02:31+00:00Added an answer on May 31, 2026 at 4:02 pm

    The Function object can be used to dynamically generate functions.

    var some_fixed_value = 1;
    var f = new Function("return " + some_fixed_value);
    

    Opposed to:

    var some_fixed_value = 1;
    var f = function() {
        return some_fixed_value;
    };
    // Break the function logic:
    some_fixed_value = 'not_fixed_any_more!';
    

    or, using two function expressions:

    var f = (function(copy_of_fixed_value) {
        return function() {
            return copy_of_fixed_value;
        };
    })(some_fixed_value);
    

    “This can also be solved with eval“:

    var func_func = new Function('x', 'return x');  //function anonymous(x){return x}
    var evil_func = eval('function(x){return x}');  //SyntaxError: function statement
                                                    // requires a name
    var eval_func = eval('(function(x){return x})');//function(x){return x}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if is it there any easy, and common between .NET and Mono,
Is there an easy way to create a class that is derived from IDispatch
Most common ORMs implement persistence by reachability, either as the default object graph change
A common piece of code I use for simple string splitting looks like this:
I would like to create a set of custom controls that are basically image
There are many IDEs to create database schema pictorially. I was wondering as to
My new assignment at work is to create a second version of our existing
my problem is easy to explain with an example: I have a 'common' schema
I would like to create a web application(maybe a shopping cart) in ASP.NET and
I feel like this is a stupid question because it seems like common sense

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.