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

The Archive Base Latest Questions

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

I know this will work: function Foo() {}; Foo.prototype.talk = function () { alert(‘hello~\n’);

  • 0

I know this will work:

function Foo() {};
Foo.prototype.talk = function () {
    alert('hello~\n');
};

var a = new Foo;
a.talk(); // 'hello~\n'

But if I want to call

Foo.talk() // this will not work
Foo.prototype.talk() // this works correctly

I find some methods to make Foo.talk work,

  1. Foo.__proto__ = Foo.prototype
  2. Foo.talk = Foo.prototype.talk

Are there other ways to do this? I don’t know whether it is right to do so. Do you use class methods or static methods in your JavaScript code?

  • 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-26T05:16:50+00:00Added an answer on May 26, 2026 at 5:16 am

    First off, remember that JavaScript is primarily a prototypal language, rather than a class-based language1. Foo isn’t a class, it’s a function, which is an object. You can instantiate an object from that function using the new keyword which will allow you to create something similar to a class in a standard OOP language.

    I’d suggest ignoring __proto__ most of the time because it has poor cross browser support, and instead focus on learning about how prototype works.

    If you have an instance of an object created from a function2 and you access one of its members (methods, attributes, properties, constants etc) in any way, the access will flow down the prototype hierarchy until it either (a) finds the member, or (b) doesn’t find another prototype.

    The hierarchy starts on the object that was called, and then searches its prototype object. If the prototype object has a prototype, it repeats, if no prototype exists, undefined is returned.

    For example:

    foo = {bar: 'baz'};
    console.log(foo.bar); // logs "baz"
    
    foo = {};
    console.log(foo.bar); // logs undefined
    
    function Foo(){}
    Foo.prototype = {bar: 'baz'};
    f = new Foo();
    console.log(f.bar);
    // logs "baz" because the object f doesn't have an attribute "bar"
    // so it checks the prototype
    f.bar = 'buzz';
    console.log( f.bar ); // logs "buzz" because f has an attribute "bar" set
    

    It looks to me like you’ve at least somewhat understood these “basic” parts already, but I need to make them explicit just to be sure.

    In JavaScript, everything is an object3.

    everything is an object.

    function Foo(){} doesn’t just define a new function, it defines a new function object that can be accessed using Foo.

    This is why you can access Foo‘s prototype with Foo.prototype.

    What you can also do is set more functions on Foo:

    Foo.talk = function () {
      alert('hello world!');
    };
    

    This new function can be accessed using:

    Foo.talk();
    

    I hope by now you’re noticing a similarity between functions on a function object and a static method.

    Think of f = new Foo(); as creating a class instance, Foo.prototype.bar = function(){...} as defining a shared method for the class, and Foo.baz = function(){...} as defining a public static method for the class.


    ECMAScript 2015 introduced a variety of syntactic sugar for these sorts of declarations to make them simpler to implement while also being easier to read. The previous example can therefore be written as:

    class Foo {
      bar() {...}
    
      static baz() {...}
    }
    

    which allows bar to be called as:

    const f = new Foo()
    f.bar()
    

    and baz to be called as:

    Foo.baz()
    

    1: class was a “Future Reserved Word” in the ECMAScript 5 specification, but ES6 introduces the ability to define classes using the class keyword.

    2: essentially a class instance created by a constructor, but there are many nuanced differences that I don’t want to mislead you

    3: primitive values—which include undefined, null, booleans, numbers, and strings—aren’t technically objects because they’re low-level language implementations. Booleans, numbers, and strings still interact with the prototype chain as though they were objects, so for the purposes of this answer, it’s easier to consider them “objects” even though they’re not quite.

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

Sidebar

Related Questions

I know downcasting like this won't work. I need a method that WILL work.
I know that this feature will be deprecated in C++0x, but for me as
I know there are a few regex/ lastIndex discrepancies but this one is new
I know this will be a difficult question, so I am not necessarily looking
If so why? I know setting this on will result in more number of
I know one awkward solution for this taks will be : first use ct
I'm working on a menu-generating HtmlHelper extension method. This method will need to know
Would anyone happen to know a trick that will keep this MSBuild task from
Know this might be rather basic, but I been trying to figure out how
I know this might be a no-brainer, but please read on. I also know

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.