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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:14:35+00:00 2026-06-02T07:14:35+00:00

Today I saw two different types of Javascript function declarations and I’d like to

  • 0

Today I saw two different types of Javascript function declarations and I’d like to have a deeper understanding of the two:

function Car( model, year, miles ){
   this.model = model;
   this.year    = year;
   this.miles  = miles;
}

/*
 Note here that we are using Object.prototype.newMethod rather than 
 Object.prototype so as to avoid redefining the prototype object
*/
Car.prototype.toString = function(){
    return this.model + " has done " + this.miles + " miles";
};

var civic = new Car( "Honda Civic", 2009, 20000);
var mondeo = new Car( "Ford Mondeo", 2010, 5000);

console.log(civic.toString());

and type 2:

function Car( model, year, miles ){
   this.model = model;
   this.year    = year;
   this.miles  = miles;
   this.toString = function(){
       return this.model + " has done " + this.miles + " miles";
   };
}


var civic = new Car( "Honda Civic", 2009, 20000);
var mondeo = new Car( "Ford Mondeo", 2010, 5000);

console.log(civic.toString());

Specifically the ‘prototype’ and the ‘this.toString’.

Can anyone impart some pearls of JS wisdom?

  • 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-02T07:14:36+00:00Added an answer on June 2, 2026 at 7:14 am

    The primary difference here is that in method 2 you are redefining the method with every new instance of Car you create, which is technically less performant.

    One nice thing method 2 does afford you however is that you can create truly private instance variables, like so:

    function Person( _age ){
        var age = _age;
        this.canDrink = function(){
            return age >= 21;
        }
    }
    
    var p = new Person(25);
    p.canDrink() // true
    p.age // undefined, because age is not exposed directly
    

    Another advantage to method 1 (besides performance) is that you can now change the functionality of all instances of on object. For example:

    function Person( _age ){
        this.age = _age;
    }
    Person.prototype.canDrink = function(){
        return this.age >= 21;
    }
    
    var a = new Person(15),
        b = new Person(25);
    a.canDrink() // false
    b.canDrink() // true
    
    Person.prototype.canDrink = function(){ return true }
    a.canDrink() // true
    b.canDrink() // true
    

    this would not be possible with method 2 (without changing it for every instance). Age however, is now exposed:

    a.age // 15
    b.age // 25
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I saw a JavaScript syntax (when invoking a function) that is unfamiliar to
I was looking through some code today and saw something like the following: var
I have heard that C doesn't have closure, and today I saw the use
I saw something like this today: frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
I have always written my JavaScript blocks var functionName = function() { if (someCondition)
I saw a website today that stated: Share/Like this site and get 10% discount.
I saw two url's for jquery today http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2 and http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js Which one should i
I was playing around with NSCalendar and NSDateComponents today and I saw constants like
Today I saw in a Java application several different approaches to load a file.
Today I saw a neat copy function in VI, in which you could copy

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.