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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:33:02+00:00 2026-06-15T17:33:02+00:00

I understand that it is good practice to assign methods directly to a class’

  • 0

I understand that it is good practice to assign methods directly to a class’ prototype so that they are not redefined every time the function is called.

var A = function () {
    this.prop = "A property";
}
A.prototype.method = function () {
    return "A method";
}

Say I want to call a method defined in this way in the constructor. Is this possible?

var A = function (options) {
    initialize(options); // initialize is undefined.
}
A.prototype.initialize = function (options) {
    // do something with options.
}

Creating a stub method inside the constructor doesn’t work because it gets called instead of the prototype’s version of the function. The only way I have been able to get this to work is to reference the function via bracket syntax this["initialize"]() which seems pretty inelegant.

var A = function (options) {
    this["initialize"](options); // function isn't defined yet but will be soon!
}
A.prototype.initialize = function (options) {
    // do something with options.
}

This seems pretty janky and would probably not be the most optimal way to call this function. Is there another way or am I missing something?

  • 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-15T17:33:02+00:00Added an answer on June 15, 2026 at 5:33 pm

    Your last way is correct, but you don’t need the quotes.

    var A = function (options) {
        this.initialize(options); 
    };
    A.prototype.initialize = function (options) {
        // do something with options.
    };
    

    Assuming A is invoked as a constructor, this is a reference to the new object being constructed, and its prototype chain is already established, so it already has access to the initialize method.

    Of course there’s no requirement that initialize be prototyped. If it’s not intended to be called more than once, then it may make more sense to define it as a stand alone function.

    var A = function (options) {
        initialize(this, options); 
    };
    function initialize (obj, options) {
        // do something with options.
    };
    

    or you could just put the initialization code right in the constructor function.

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

Sidebar

Related Questions

I understand that it's completely wrong and is not a good practice, but I
I understand that it is good practice to use a using block when getting
So I've always been taught that good coding practice is to use accessor methods
I know that using side effects in Python list comprehensions is not good practice.
I feel, every time I read a C or C++ program, that half or
I understand that in this code: class Foo { public static void method() {
I understand that one benefit of having static member functions is not having to
I understand that: '\n' // literally the backslash character followed by the character for
I understand that JVM and CLR were designed as stack-based virtual machines. When JIT
I understand that the em measurement is a relative unit for font-size, relative to

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.