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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:03:32+00:00 2026-05-13T14:03:32+00:00

I would like to create a Javascript class that I can use like so:

  • 0

I would like to create a Javascript class that I can use like so:

var f = new myFunction(arg1, arg2);
f.arg1; // -> whatever was passed as arg1 in the constructor
f();
f instanceof myFunction // -> true
typeof f // -> function

I can treat it like a normal object, even adding the native Function object to the prototype chain, but I can’t call it like a function:

function myFunction(arg1, arg2) {
  this.arg1 = arg1;
  this.arg2 = arg2;
}
myFunction.prototype = Function
var f = new myFunction(arg1, arg2); // ok
f.arg1; // ok
f(); // TypeError: f is not a function, it is object.
f instanceof myFunction // -> true
typeof f // -> object

I can have the constructor return a function, but then it’s not an instance of myFunction:

function myFunction(arg1, arg2) {
  var anonFunction = function() {
  };
  anonFunction.arg1 = arg1;
  anonFunction.arg2 = arg2;
  return anonFunction;
}
var f = new myFunction(arg1, arg2); // ok
f.arg1; // ok
f(); // ok
f instanceof myFunction // -> false
typeof f // -> function

Any suggestions? I should add that I really want to avoid using new Function() since I hate string code blocks.

  • 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-13T14:03:33+00:00Added an answer on May 13, 2026 at 2:03 pm

    First and foremost, you should probably be considering some other way of doing this, because it is unlikely to be portable. I’m just going to assume Rhino for my answer.

    Second, I’m not aware of any way in Javascript of assigning a function’s body after construction. The body is always specified as the object is constructed:

    // Normal function definitions
    function doSomething() { return 3 };
    var doSomethingElse = function() { return 6; };
    
    // Creates an anonymous function with an empty body
    var doSomethingWeird = new Function;
    

    Now, there’s a non-standard Mozilla extension in the form of a __proto__ property on every object. This allows you the change the inheritance chain of any object. You can apply this to your function object to give it a different prototype after construction:

    // Let's define a simple prototype for our special set of functions:
    var OddFunction = {
      foobar: 3
    };
    
    // Now we want a real function with this prototype as it's parent.
    // Create a regular function first:
    var doSomething = function() {
      return: 9;
    };
    
    // And then, change it's prototype
    doSomething.__proto__ = OddFunction;
    
    // It now has the 'foobar' attribute!
    doSomething.foobar;  // => 3
    // And is still callable too!
    doSomething();  // => 9
    
    // And some of the output from your example:
    doSomething instanceof OddFunction;  // => true
    typeof doSomething;  // => function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From various reading it appears that CFNetwork is responsible for… May 14, 2026 at 7:40 pm
  • Editorial Team
    Editorial Team added an answer http://www.tsjensen.com/blog/CommentView,guid,904052d4-8ee0-47b5-bacb-eb1788137233.aspx May 14, 2026 at 7:40 pm
  • Editorial Team
    Editorial Team added an answer Change this line: if (node.HasChildNodes) FindNode(node.ChildNodes, nodeName); to: if (node.HasChildNodes)… May 14, 2026 at 7:40 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.