JavaScript Methods Worksheet

Question 1

How is a method different from a regular function in JavaScript?

A method is a function that is associated with an object. It is defined as a property of the object and can operate on the data contained within that object. In contrast, a regular function is not tied to any specific object and can be called independently.

Question 2

Why would we want to add methods to an object?

Methods allow us to define behaviors for objects, enabling the objects to perform actions and manipulate their own data. This helps organize code and makes it easier to work with complex data structures.

Question 3

How can we access the property of an object from inside the body of a method of that object?

We can access the property of an object from inside the body of a method of that object using the 'this' keyword. For example, if the object has a property named 'name', we can access it inside a method using 'this.name'.

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.