Explore Function in an Object
Function in an Object
Let's explore functions in an object
Explore:
- Use the addTwoToThisNum function in the mathyObject.
- Add a function to the mathyObject and use it.
- Access the model of the aCar object.
- Use the pressHorn function from aCar.
- Add a function to the aCar and use it.
- 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 mathyObjectconsole.log(mathyObject.feelingsAboutMath);// Here we use the addFunction functionconsole.log(mathyObject.addFunction(1, 4));// Here we use the squareThisNumber functionconsole.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 aCarconsole.log(aCar.make);// Here we use the addFunction functionconsole.log(aCar.playMusic('Jimi Hendrix'));// variables and functions - not using objectslet howDoIFeelAboutMath = 'Math is coooool!';let additionFunction = function(argumentOne, argumentTwo){return argumentOne + argumentTwo;}console.log(howDoIFeelAboutMath);console.log(additionFunction(22, 45));