Object Oriented Programming - Use a Mixin to Add Common Behavior Between Unrelated Objects

Tell us what’s happening:
I’ve added the glide method to the objects, but it will not pass the tests. The only difference I see between my code and the provided solution is that the solution adds glide as a function that prints out a string. Why is it necessary to make the method a function?

Your code so far

let bird = {
  name: "Donald",
  numLegs: 2
};

let boat = {
  name: "Warrior",
  type: "race-boat"
};

// Only change code below this line

let glideMixin = function(object) {
  object.glide = true;
}

glideMixin(bird)
glideMixin(boat)

console.log(bird.glide)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36

Challenge: Object Oriented Programming - Use a Mixin to Add Common Behavior Between Unrelated Objects

Link to the challenge:

Generally method refers to a function. See:

1 Like

As said, methods are just functions on object properties. The new “functionality” you are adding to the object is the function.

glideMixin is just a function that takes in the object, the object is then extended with a property that is set to a function with the desired new functionality.

Here is a more generalized abstraction.

let bird = {
  name: "Donald",
  numLegs: 2,
};

function objectExtender(objToExtend, property, value) {
  objToExtend[property] = value;
}

function flyFunctionality() {
  console.log("Flying");
}

function quackFunctionality() {
  console.log("Quacking");
}

objectExtender(bird, "fly", flyFunctionality);
bird.fly(); // Flying

objectExtender(bird, "quack", quackFunctionality);
bird.quack(); // Quacking

objectExtender(bird, "age", 5);
console.log(bird.age); // 5

Thanks, for some reason I was thinking of “method” as just any property of an object.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.