From 3efacddb00a0cf3f1b2bbdfeb825d8fdf2d3c63c Mon Sep 17 00:00:00 2001 From: Clarence Bakosi Date: Thu, 30 Oct 2025 12:16:00 +0100 Subject: [PATCH] feat(curriculum): Add interactive examples to What Is the Difference Between Functions and Object Methods (#63310) Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com> --- .../6732b749b8aad125523dcda5.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/curriculum/challenges/english/blocks/lecture-introduction-to-javascript-objects-and-their-properties/6732b749b8aad125523dcda5.md b/curriculum/challenges/english/blocks/lecture-introduction-to-javascript-objects-and-their-properties/6732b749b8aad125523dcda5.md index bd9043bb9ff..a72ad0e518c 100644 --- a/curriculum/challenges/english/blocks/lecture-introduction-to-javascript-objects-and-their-properties/6732b749b8aad125523dcda5.md +++ b/curriculum/challenges/english/blocks/lecture-introduction-to-javascript-objects-and-their-properties/6732b749b8aad125523dcda5.md @@ -5,12 +5,14 @@ challengeType: 19 dashedName: what-is-the-difference-between-functions-and-object-methods --- -# --description-- +# --interactive-- In JavaScript, functions and object methods are both ways to encapsulate reusable code, but they have some key differences in how they are defined, used, and the context in which they operate. Understanding these differences is crucial for writing effective and organized JavaScript code. As you learned in earlier modules, functions are reusable blocks of code that perform a specific task: +:::interactive_editor + ```js function greet(name) { return "Hello, " + name + "!"; @@ -18,8 +20,12 @@ function greet(name) { console.log(greet("Alice")); // "Hello, Alice!" ``` +::: + Object methods, on the other hand, are functions that are associated with an object. They are defined as properties of an object and can access and manipulate the object's data. Here's an example of an object with a method: +:::interactive_editor + ```js const person = { name: "Bob", @@ -32,6 +38,8 @@ const person = { console.log(person.sayHello()); // "Hello, my name is Bob" ``` +::: + In this example, `sayHello` is a method of the `person` object. The `this` keyword allows the `sayHello` method to access the properties of the object named `person`. You will learn more about the `this` keyword in future lessons. A difference between functions and methods is how they are invoked. Functions are called by their name, while methods are called using dot notation on the object they belong to. For example, we call the `greet` function as `greet("Alice")`, but we call the `sayHello` method as `person.sayHello()`.