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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:43:35+00:00 2026-05-28T16:43:35+00:00

function Foo(){…} Foo.bar = function (){…}; Is this the only pattern for adding a

  • 0
function Foo(){...}
Foo.bar = function (){...};

Is this the only pattern for adding a static method to a constructor function? In particular, is it not possible to create the static method bar() within the definition of Foo() itself?

  • 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-28T16:43:36+00:00Added an answer on May 28, 2026 at 4:43 pm

    When you say “inside”, it sounds like you need a clean way to keep everything in one place. You could potentially use a class inheritance library that has support for static declarations. Or simply take one and extend it yourself to add that capability.

    For a simple (but not so compact) way to keep everything together, you could go with something like this:

    var Foo = (function () {
        var ctor = function () {
            // the constructor
        };
    
        ctor.staticMethod = function () {
            // something static
        };
    
        return ctor;
    })();
    

    But! How important really is making the declaration self-evident that it is static? You could simply declare your static methods as prototype methods and convey the fact that they are static (i.e. not acting on the instance) methods with some code comments. There won’t be any contractual enforcement of how these methods are invoked, but there will be few side-effects. So I would just go with:

    function Foo() {
        // the constructor
        // optionally define instance methods here
    }
    
    Foo.prototype = {
        instanceMethod: function () {
            // some instance method
            // this.bar(); ...
        },
        staticMethod: function () {
            // some static method
            // return 2 + 3;
        }
    };
    

    Usage:

    // Using "prototype" explicitly can be your contract for saying "this is static"
    var sum = Foo.prototype.staticMethod();
    
    var inst = new Foo();
    
    var sum2 = inst.staticMethod(); // You have the added benefit of being able to call your static methods on instances
    

    I’ve found that the above comes in handy especially when you’re using the factory design pattern. Your class can have some static factory methods in its prototype and you can invoke these factory methods even when you only have an instance whose origin class you don’t know.

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

Sidebar

Related Questions

function Foo() { this.SayFoo = function() { console.log('Foo'); }; } function Bar() { this.SayBar
My question is: function Foo() { this.foo = bar; // <- What is this
for example in PHP class foo{ function foo($name){ //constructor $this->name=$name; } function sayMyName(){ return
If I have a function like this: function foo(_this) { console.log(_this); } function bar()
function Foo() { var myPrivateBool = false, myOtherVar; this.bar = function(myOtherVar) { myPrivateBool =
In javascript given this three constructor functions: function Foo(data) { var _data = data;
Im trying to create this with the php encode function: { foo: [ {
function foo() {} var bar = foo <| function() {}; This is the first
function Foo() { alert(this === a);//return false! **why this is not equal a?** }
how to do this function foo(x:*, ...args):* { } function bar(x:*, ...args):* { foo(x,

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.