JS Passing argument

上一篇 / 下一篇  2015-08-16 09:11:06 / 个人分类:JavaScript

Passing Arguments 

The askTeller function has been modified within the Person class to directly give you your balance. However, it now needs the account password in order to return the bankBalance.

Instructions

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

Sample Codes as below:

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);




TAG:

wilber.shinobi的个人空间 引用 删除 wilber.shinobi   /   2015-08-16 11:40:31
Examine the languages object. Three properties are strings, whereas one is a number.

Use a for-in loop to print out the three ways to say hello. In the loop, you should check to see if the property value is a string so you don't accidentally print a number.
wilber.shinobi的个人空间 引用 删除 wilber.shinobi   /   2015-08-16 11:40:00
Looks For-In To Me
Objects aren't so foreign if you really think about it!

Remember you can figure out the type of a variable by using typeof myVariable;. Types we are concerned with for now are "object", "string", and "number".

Recall the for-in loop:

for(var x in obj) {
executeSomething();
}
This will go through all the properties of obj one by one and assign the property name to x on each run of the loop.

Let's combine our knowledge of these two concepts.

Sample codes:
var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for ( var x  in languages ) {
    if ( (typeof languages[x]) === "number" ) {
        continue; // to skip printing out the non string.
    } else if ( (typeof languages[x]) === "string" ) {
        console.log(languages[x]);
    }
}
Results:
Hello!
Bonjour!
Hola!
 

评分:0

我来说两句

Open Toolbar