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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:44:52+00:00 2026-06-15T15:44:52+00:00

I have js object like this: var Dog = function(dogName) { this.bark = function()

  • 0

I have js object like this:

var Dog = function(dogName) {
  this.bark = function() {
    console.log(dogName + " is barking");
  }
}

and

var Dog = function(dogName) {
  this.dogName = dogName;
  this.bark = function() {
    console.log(this.dogName + " is barking");
  }
}

I can use both the same way:

var puppy = new Dog("Ringo");
puppy.bark();

My question is is there any practical difference between those two approaches? Is it better to assign constructor parameters to this.<field>, or I can just utilize those parameters straight away as they are accessible to inner functions? Are there any special cases for both?

  • 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-15T15:44:53+00:00Added an answer on June 15, 2026 at 3:44 pm

    The difference is what happens when you say

    var puppy = new Dog("Ringo");
    puppy.dogName = "George";
    puppy.bark();
    

    With the first approach, it will still use the name “Ringo” and with the second approach it will say “George”. For the same reason if you try to say

    var puppy = new Dog("Ringo");
    console.log("created new puppy: " + puppy.dogName);
    

    that will work with the second version but not the first. So if your goal is to make dogName effectively private so that it can’t be accessed or changed after being initialized, you should go with the first approach, otherwise always go with the second approach.

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

Sidebar

Related Questions

I have javascript object defined like this: function SocialMiner() { var verbose=true; var profileArray=new
I have an object like this: var someObj = Class.create ({ initialize: function(objName){ this.objName
I have two object literals: var animal = { eat: function() { console.log(eating...); }
I have an object literal like this: var test = { one: function() {
I have an constructor object like this . var Cons = function(me){ this.name= me;
I have a LINQ query that returns some object like this... var query =
When I have a JavaScript object like this: var member = { mother: {
I have an IQueryable object like this var persons = from m in db.Persons
I have a certain object created like this: var classes = { login: {
I have an object like this: var queryObject= { name: 'Shwetanka', subjects: ['Mathematics', 'Physics',

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.