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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:14:19+00:00 2026-05-22T22:14:19+00:00

Basically I looking for the ability to attach methods to an executable function while

  • 0

Basically I looking for the ability to attach methods to an executable function while using the javascript prototype method. The code below demonstrates want I’m talking about and the functionality I’m looking for, but it is really a hack. Notice I have a valid this object to attach variables along with a main and init function.

function create(){
    var $this = {},
    main = function(){
        prototype.main.apply($this,arguments);
    };
    prototype.init.apply($this,arguments);
    //Add additional prototype methods by brute force, ugly
    for(i in prototype)-function(i){
        main[i]=function(){
            prototype[i].apply($this,arguments);
        }
    }(i);
    return main;
};

var prototype = {
    //called when you create the object
    init:function(text){
        console.log('init');
        this.text = text;
    },
    //called when you call the object
    main:function(){
        console.log('main');
        console.log(this);
    },
    method:function(){
        console.log(this.text);
    }
};

//create returns a function that also has methods
//the below line will call the init method
var fun = create('some variables');
//call main function
fun();
//call methods
fun.method();

I’m afraid I might be missing something obvious.

Here is the same functionality as above, but instead extends the global function prototype.

Extending the global properties is bad practice, so I am looking for a alternative solution.

Function.prototype = {
    //called when you create the object
    init:function(text){
        console.log('init');
        this.text = text;
    },
    //called when you call the object
    main:function(){
        console.log('main');
        console.log(this);
    },
    method:function(){
        console.log(this.text);
    }
};

function create(){
    var ret = function(){
        ret.main.call(main);
    };
    ret.init.apply(main,arguments);
    return ret;
};

//create returns a function that also has methods
//the below line will call the init method
var fun = create('some variables');
//call main function
//fun();
//call methods
fun.method();

Just as an obvious point, it doesn’t appear you can use the typical new object approach because if you call new you can’t return a separate value.

Any explanation or considerations would be great!

  • 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-22T22:14:20+00:00Added an answer on May 22, 2026 at 10:14 pm

    You can put your the prototype functions into the “constructor” body. This technically is what you are currently doing, but defining them explicitly rather than using a helper method is much cleaner. Then, you can further simplify your code using the following pattern for public and private variables and methods:

    function Fun(text) {
        // This is the main function
        var fn = function () {
            return 'main';
        };
    
        // Attach public variables and methods
        fn.publicVariable = 'public';
        fn.publicMethod = function () {
            return text; // text is a "private variable"
        };
    
        // Do whatever initialization
        console.log('init');
    
        // Return the main function     
        return fn;
    }
    
    var fun = Fun('this is some text'); // "init"
    fun() // "main"
    fun.publicMethod() // "this is some text"
    console.log(fun.publicVariable); // "public"
    console.log(fun.text); // undefined
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My team's current project involves re-writing retrieval libraries in JavaScript. We are basically looking
Basically I am looking for a win32 method to invoke in C# to set
Basically I'd be looking to implement a method like this. IQueryAble GetQuery<T>(Entities db) or
Basically the questions in the title. I'm looking at the MVC 2 source code:
I am looking for javascript/css editors that are a good alternative to using aptana.
I'm basically looking for a way to access a hashtable value using a two-dimensional
I'm looking for the ability to convert entire methods into Expression trees. Writing it
I'm basically looking for a way to automate typing stuff like the following: cout
Basically just looking to see if you can capture an image from the webcam
Basically, I'm looking for something that will allow me to replicate the following Perl

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.