26.Passing Arguments


#1

It is strange,but it works...
Why?
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;

this.askTeller = function(pass) {
if (pass == 1234) return bankBalance;
else return "Wrong password.";
};
}

var john = new Person('John','Smith',30);
/* the variable myBalance should access askTeller()
with a password as an argument */
var myBalance = function(pass=1234) {
return askTeller; }


#2

I've remade it...
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;

   this.askTeller = function(pass) {
     if (pass == 1234) return bankBalance;
     else return "Wrong password.";
   };
}

var john = new Person('John','Smith',30);
/* the variable myBalance should access askTeller()
   with a password as an argument  */
var myBalance = john.askTeller(1234); 

   console.log(myBalance);

It works too.


#3

What's strange?
Did you expect something else? Why?
What information are you looking for?

If it doesn't behave as you expect, consider which particular operation(s) are involved and find out more about them

(Be more specific about what you're after)


#4

Why was there no fault in the first example of the code? I didn't write john.function in the end.


#5

What do you mean by fault?
And what do you mean write john.function? Do you mean calling the function and printing the result?
Should you have done that according to the instructions? Did it happen?

Is this the instructions? (I don't know which exercise you're on)

Create a new variable called myBalance that calls the askTeller
function with a password argument, 1234.

If so, it says nothing about printing, right? So that it isn't wrong because it doesn't print isn't weird since that wasn't asked for.

However, you're also not doing what it says in all your versions. So when you say "works", you probably mean that you get a green banner, not that it's doing what it's supposed to. ("works" is a word to avoid when describing something, because it doesn't desecribe anything, it requires already knowing what you mean)


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.