This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by t3321
over 8 years

24/30 geting confused whti "this"

I´m getting confused whit “this” maybe somebody could give me a hint. i understand that insider a constructor “this” is used to assign the “input” value to the value insider the constructor.

But what happens here: this.getBalance = function() what technical “this” means here? and even more general what does “this” do?

  function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      // your code should return the bankBalance
      return (bankBalance);
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance();

console.log(myBalance);

the code will also work when i remove “this” from the constructor.

function Person(first,last,age) {
  firstname = first;
  lastname = last;
  age = age;
   var bankBalance = 7500;
  
   this.getBalance = function() {
      // your code should return the bankBalance
      return (bankBalance);
   };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance()
var myBalance = john.getBalance();

console.log(myBalance);

Answer 55f52b2793767627f800093f

2 votes

Permalink

Have a read through http://stackoverflow.com/questions/4354418/var-vs-this-vs-constructor-parameter-variables and http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/

[quote] Yes, the difference is in how the variable is stored.

The variable declared with var is local to the constructor function. It will only survive beyond the constructor call if there is any function declared in the scope, as it then is captured in the functions closure.

The variable declared with this. is actually not a variable, but a property of the object, and it will survive as long as the object does, regardless of whether it’s used or not.

Edit: If you are using variables without declaring them, they will be implicitly declared in the global scope, and not part of the object. Generally you should try to limit the scope for what you declare, so that not everything end up in the global scope.

points
Submitted by Leon
over 8 years

1 comments

t3321 over 8 years

hey Leon Thanks will have a read on that!

Answer 560c2b993e0ec82fb3000091

0 votes

Permalink

function Person(first,last,age) { this.firstname = first; this.lastname = last; this.age = age; var bankBalance = 7500;

this.getBalance = function() { // your code should return the bankBalance return bankBalance; }; }

var john = new Person(‘John’,’Smith’,30); console.log(john.bankBalance);

// create a new variable myBalance that calls getBalance() var myBalance = john.getBalance();

console.log(myBalance);

points
Submitted by Paul Frol
over 8 years