JavaScript Methods Worksheet
Question 1
How is a method different from a regular function in JavaScript?
A method is just a function that belongs to an object, like an action the object can do. So, if you have a car object, a method might be “start” — it something the car can do.
A regular function isnt tied to any specific object; it just exists on its own and can be used anywhere in your code.
The main difference? A method is attached to an object and can use this to refer back to that object, while a regular function isn attached to anything specific.
A regular function isnt tied to any specific object; it just exists on its own and can be used anywhere in your code.
The main difference? A method is attached to an object and can use this to refer back to that object, while a regular function isn attached to anything specific.
Question 2
Why would we want to add methods to an object?
Adding methods to an object lets that object perform actions related to itself.
This is helpful because it keeps related data and behavior together, making your code easier to understand and work with.
Question 3
How can we access the property of an object from inside the body of a method of that object?
To access a property of an object from inside one of its methods, you use this. The keyword this refers to the object itself, so you can use it to access other properties or methods within the same object.
Coding Problems
Coding Problems - See the 'script' tag below this h3 tag. You will have to write some JavaScript code in it.
Always test your work! Check the console log to make sure there are no errors.