// ch26 Objects and Methods
var john = {
firstName: "John",
//key: value
lastName: "Smith",
birthYear: 1990,
family: ["jane", "Mark", "Bob"],
job: "teacher",
isMarried: false,
calcAge: function(){//function expression// method of 'john'
this.age = 2018-this.birthYear;
}
};
john.calcAge();
console.log(john);//print
/*
firstName: "John"
lastName: "Smith"
birthYear: 1990
family: (3) ["jane", "Mark", "Bob"]
job: "teacher"
isMarried: false
calcAge: ƒ ()
age: 28
*/
// ch28 Objects and Methods coding challange
var john ={
firstName:"John",
mass:92,
height: 1.95,
calcBMI: function() {
this.bmi = this.mass/(this.height*this.height);
return this.bmi;
}
}
var mark ={
firstName:"Mark",
mass:95,
height: 1.75,
calcBMI: function(){
this.bmi = this.mass/(this.height*this.height);
return this.bmi;
}
}
if (john.calcBMI>mark.calcBMI){
console.log("John's BMI("+john.bmi+") is higher then Mark's");
} else if (john.calcBMI<mark.calcBMI){
console.log("Mark's BMI("+mark.bmi +") is higher then John's");
}else{
console.log("they have some BMIs")
}
'javascript' 카테고리의 다른 글
JavaScript/ Hoisting, Function and Variable (0) | 2020.02.14 |
---|---|
JavaScript/ Loops and Iteration (0) | 2020.02.13 |
JavaScript/ Object and Properties (0) | 2020.02.13 |
JavaScript/ Array Basic (0) | 2020.02.13 |
JavaScript/ Function Basic (0) | 2020.02.13 |