0

While I was going through JavaScript functions, I've came across following code which works fine

var Dog = function(name, breed) {
   this.name = name;
   this.breed = breed;
   console.log(this.name);
};
var firstDog = new Dog("munni","doberman");

Then I try to play around it and got stuck with this code

var Dog = function(name, breed) {
   var name = name;
   var breed = breed;
   console.log(this.name);
};
var firstDog = new Dog("munni","doberman");

In this code when I remove this, It works fine. I understood that this points to the current object. But I'm not able to figure it out what's going on here. Should I always use this operator while initializing properties in constructors? I just want to know best practice for this since I'm a beginner. Any helpful answer will be appreciated.

Kishore Kumar Korada
  • 1,204
  • 6
  • 22
  • 47

2 Answers2

0

When using this. you are assigning public properties of the current object. "this" will point to the object you are intanciating when using the "new" keyword. When using "var" they like private variables of your instance.

slvnperron
  • 1,323
  • 10
  • 13
  • So, If I use it as var and as you said it's like private variables in an Instance, Is there a way to access such variables from outer part of the instance? I'm actually a Java Developer and just started learning JS so :) – Kishore Kumar Korada Jan 26 '15 at 03:38
  • 1
    There is no concept of classes in javascript. Everything is a function so it's not a real 'instance private variable'. So you really need to be within the variable declaration scope to access it. – slvnperron Jan 26 '15 at 03:41
  • Thank You for your help. But I didn't understand the last line in your comment which says " I really need to be within the variable declaration scope to access it". Can you provide me any references for that? or any code? Which could be help me alot – Kishore Kumar Korada Jan 26 '15 at 03:45
  • Sorry about the confusion. I meant that you can access it only from the same or inner declaration scope. You can read more about scopes here: http://www.w3schools.com/js/js_scope.asp – slvnperron Jan 26 '15 at 03:48
-1

When you type this.name, you are referring to the current object "Dog", but when you have done "var name = name;" you basically created a new local object "name" which is not the same object as "this". I'm a C# developer, but it should be the same in javascript.

Dan
  • 26
  • 4
  • This is correct. var name = ... is creating a private variable inside the local function, while this.name attaches the variable as an attribute to the parent Dog object. – ryanlutgen Jan 26 '15 at 02:05
  • @Vizkos: I would avoid using terms like "private", because JavaScript doesn't have such concepts. `var foo;` creates a (local) variable, nothing more, nothing less. – Felix Kling Jan 26 '15 at 02:08
  • By stating "Private" it is relative to the scope of the variable, not the keyword synonymous with OO languages. The name variable's scope is local and restricted to that of the function. In short, its a loose relation to what most know of the word "private" since he is creating an object in a functional language. – ryanlutgen Jan 26 '15 at 02:10