1
  1. How can I make my min function working. I got banque is undefined?

Thanks.

  1. List item
  2. List item
  3. List item

function Personne(nom){
  this.nom = nom;
  var banque = 1500;
  this.add = function(number){
    banque = banque + number
  }
  this.getCpte = function() {
    return banque
  }
}

Personne.prototype.min = function(number){
  banque = banque - number
}

var me = new Personne('albert')



console.log(me)
me.add(500)
me.min(500) // got banque is undefined
console.log(me.getCpte())

2
  • 1
    banque doesn't exist outside of Personne function... Commented Sep 10, 2016 at 4:40
  • How can I make it exists and keeping it as a private var and using prototype method? Commented Sep 10, 2016 at 4:49

2 Answers 2

1

banque is a local variable inside Personne, and you cannot access it from outside. Either put min inside the constructor to make it a privileged method like the others, or make banque a property like .nom.

Or just do everything through the add method:

Personne.prototype.min = function(number) {
    this.add(-number);
};
Sign up to request clarification or add additional context in comments.

Comments

0

What you can do is set up a getter for banque so you can use the prototype. It seems like you've already done this with getCpte. Just use that function to get the value of banque and use that in the prototype function assignment. You will also need a setter for banque if you want to modify the value outside the Personne function (this will make banque essentially public. To combat this, set the min function in the constructor if at all possible):

function Personne(nom){
  this.nom = nom;
  var banque = 1500;
  this.add = function(number){
    banque = banque + number
  }
  this.getCpte = function() { //banque getter
    return banque
  }
  this.setCpte = function(value) { //banque setter
    banque = value;
  }  
}

Personne.prototype.min = function(number){
  this.setCpte(this.getCpte() - number) //set to banque - number
}

var me = new Personne('albert')

console.log(me)
me.add(500)
me.min(500)
console.log(me.getCpte())

What this adds is a function setCpte which is a setter for banque. This way, we can use this.setCpte(value) where value is the value being set to banque. Since you did banque - number, we can use the getter to get rid of the ReferenceError, and subtract number from it.

2 Comments

this doesn't really fix the problem, banque may be "private" - but it's value can be directly set - so, in what way is it at all private? Original code you could add, if min was done the same you could subtract, but you couldn't directly set the value - that's the extent of the "privacy" the variable had - now it's just as if the variable was exposed ... I guess now you could put some logic in the setCpte function to sanitise the set value, but that's about all you're getting from this
@JaromandaX That's an huge caveat that should be noted, but I'm not sure there's any other way to do it with the OP trying to have it set in a prototype. I would just recommend setting it in the Personne function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.