Explore Function in an Object

Function in an Object

Let's explore functions in an object

Explore:

  1. Use the addTwoToThisNum function in the mathyObject.
  2. Add a function to the mathyObject and use it.
  3. Access the model of the aCar object.
  4. Use the pressHorn function from aCar.
  5. Add a function to the aCar and use it.
  6. How are functions and variables off and on objects similar and different?
let mathyObject = {
feelingsAboutMath: 'I loooove math!',
addFunction: function(x, y) {
return x + y;
},
squareThisNumber: function(x) {
return x * x;
},
addTwoToThisNum: function(a) {
return a + 2;
}
}
// Here we access a value from the mathyObject
console.log(mathyObject.feelingsAboutMath);
// Here we use the addFunction function
console.log(mathyObject.addFunction(1, 4));
// Here we use the squareThisNumber function
console.log(mathyObject.squareThisNumber(6));
let aCar = {
make: 'Ford',
model: 'Ranger',
pressHorn: function() {
return 'honk!';
},
playMusic: function(tape){
return 'playing ' + tape;
}
}
// Here we access a value from the aCar
console.log(aCar.make);
// Here we use the addFunction function
console.log(aCar.playMusic('Jimi Hendrix'));
// variables and functions - not using objects
let howDoIFeelAboutMath = 'Math is coooool!';
let additionFunction = function(argumentOne, argumentTwo){
return argumentOne + argumentTwo;
}
console.log(howDoIFeelAboutMath);
console.log(additionFunction(22, 45));