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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:00:00+00:00 2026-06-10T22:00:00+00:00

What’s the difference between these two method of defining a ‘class’ in JavaScript? Method

  • 0

What’s the difference between these two method of defining a ‘class’ in JavaScript?

Method One

Define method within the constructor:

function MyClass()
{
    this.foo = function() { console.log('hello world!'); };
}

Method Two

Define method on the prototype:

function MyClass()
{}

MyClass.prototype.foo = function() { console.log('hello world!'); };
  • 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-10T22:00:02+00:00Added an answer on June 10, 2026 at 10:00 pm

    The first will create a new function object on each instantiation of your object, the second will assign a reference to a prototype method to each instance. In short: the second is more efficient, because all instances will share a single function object.

    That’s just the logic of a prototype chain, you can try and access anything via any object:

    var objLiteral = {foo:'bar'};
    

    When accessing objLiteral.foo JS will first look at the properties that the object itself has defined, and return the value if it is found. If JS can’t find the property on the object itself, it’ll check the object’s prototype, hence:

    objLiteral.valueOf();//method defined @Object.prototype
    objLiteral.valueOf === Object.prototype.valueOf //true
    

    But when you use your first method:

    function SomeConstructor()
    {
        this.methd = function()
        {
            return true;
        }
    }
    var f = new SomeConstructor();
    var g = new SomeConstructor();
    f.methd === g.methd;//FALSE!
    

    That shows that we’re dealing with 2 separate function objects. Move the function definition to the prototype and f.methd === g.methd; will be true:

    function SomeConstructor()
    {
    }
    SomeConstructor.prototype.methd = function()
    {
        return true;
    }
    var f = new SomeConstructor();
    var g = new SomeConstructor();
    f.methd === g.methd;//true!
    

    In response to your comment:

    Defining a method on a prototype-level allows you to change a method for a specific task, and then “reset” it back to it’s default behaviour. Suppose you’re in a function that’s creating an AJAX request:

    someObject.toString = function(){ return JSON.stringify(this);}
    //when concatinating this object you'll get its json string
    //do a lot of stuff
    delete (someObject.toString);
    

    Again JS will check if the object has the toString property defined on itself, which it has. So JS will delete the function you’ve assigned to the toString property. Next time the toString will be invoked, JS will start scanning the prototype chain all over again, and use the first occurance of the method (in the prototype). Let’s clarify:

    function SomeConstructor()
    {
    }
    SomeConstructor.prototype.methd = function()
    {
        return true;
    }
    var f = new SomeConstructor();
    var g = new SomeConstructor();
    f.methd = function(){return false;};
    g.methd();//returns true, still <-- method is gotten from the prototype
    f.methd();//returns false <-- method is defined @ instance level
    delete (f.methd);
    f.methd();//returns true, f doesn't have the method, but the prototype still does, so JS uses that.
    

    Or even better, you can even replace an instance’s method by a method from another prototype:

    f.methd = Object.prototype.valueOf;//for as long as you need
    

    the last example is pointless, because f has the valueOf method already: its inheritance chain looks like this: var f ---> SomeConstructor ---> Object, giving you access to all Object.prototype methods, too! Neat, isn’t it?

    These are just dummy examples, but I hope you see this is one of those features that make JS an incredibly flexible (sometimes too flexible, I must admit) and expressive language.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.