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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:49:47+00:00 2026-05-26T15:49:47+00:00

I like that in javascript, I can create a function, and then add further

  • 0

I like that in javascript, I can create a function, and then add further methods and attributes to that function

myInstance = function() {return 5}
myInstance.attr = 10

I would like to create a class to generate these objects. I assume I have to inherit from the Function base class.

In other words, I would like to:

var myInstance = new myFunctionClass()
var x = myInstance()
// x == 5

But I don’t know how to create the myFunctionClass. I have tried the following, but it does not work:

var myFunctionClass = function() {Function.call(this, "return 5")}
myFunctionClass.prototype = new Function()
myInstance = new myFunctionClass()
myInstance()
// I would hope this would return 5, but instead I get
// TypeError: Property 'myInstance' of object #<Object> is not a function

I also tried the more complicated (and more proper?) inheritance method found here: How to "properly" create a custom object in JavaScript?, with no more luck. I have also tried using the util.inherits(myFunctionClass, Function) found in node.js. Still no luck

I have exhausted Google, and therefore feel that I must be missing something fundamental or obvious. Help would be greatly appreciated.

  • 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-26T15:49:48+00:00Added an answer on May 26, 2026 at 3:49 pm

    Your trying to inherit from Function. This is a right pain to do. I suggest you do the following instead

    Live Example

    var Proto = Object.create(Function.prototype);
    Object.extend(Proto, {
      constructor: function (d) {
        console.log("construct, argument : ", d);
        this.d = d; 
        // this is your constructor logic
      },
      call: function () {
        console.log("call", this.d);
        // this get's called when you invoke the "function" that is the instance
        return "from call";
      },
      method: function () {
        console.log("method");
        // some method
        return "return from method";
      },
      // some attr
      attr: 42
    });
    

    You want to create a prototype object that forms the basis of your “class”. It has your generic methods/attributes. It also has a constructor that gets invoked on object construction and a call method that gets invoked when you call the function

    var functionFactory = function (proto) {
      return function () {
        var f = function () {
          return f.call.apply(f, arguments);      
        };
        Object.keys(proto).forEach(function (key) {
          f[key] = proto[key];
        });
        f.constructor.apply(f, arguments);
        return f;
      }
    }
    

    A function factory takes a prototype object and returns a factory for it. The returned function when called will give you a new function object that “inherits” from your prototype object.

    var protoFactory = functionFactory(proto);
    var instance = protoFactory();
    

    Here you create your factory and then create your instance.

    However this isn’t proper prototypical OO. we are just shallow copying properties of a prototype into a new object. So changes to the prototype will not reflect back to the original object.

    If you want real prototypical OO then you need to use a hack.

    var f = function () {
      // your logic here
    };
    f.__proto__ = Proto;
    

    Notice how we use the non-standard deprecated .__proto__ and we are mutating the value of [[Prototype]] at run-time which is considered evil.

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

Sidebar

Related Questions

I would like to create a Javascript class that I can use like so:
I know that javascript, for example supports functions inside of functions, like so: function
Here is the situation. I have some javascript that looks like this: function onSubmit()
I have a small piece of javascript that I would like to use but
I have some third-party Javascript that has statements like this: FOO = function() {
I would like to craft a case-insensitive regex (for JavaScript) that matches street names,
Ok, I have one JavaScript that creates rows in a table like this: function
In javascript you can easily create objects and Arrays like so: var aObject =
I'm trying to create a small javascript framework that can make it easier when
I know that in JavaScript, creating a for loop like this: for(int i =

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.