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

Hello everyone, i want to know why does undefined was printed in the console? Even thought i made sure that i’ve wrote the code’s syntax accurately?


let myFullName = {first: "Bushra", _$Second: "Abdulsalam", _Last: "Ghames"};

let myFullNameAgain = {first: "Bushra", _$Second: "Abdulsalam", _Last: "Ghames"};

let LogMyFullNameAgain = function(obj) {
  obj.whatever = function() {
    return "Nothing";
  }
};

console.log(LogMyFullNameAgain(myFullNameAgain));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15

Challenge Information:

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

Hello!

If this was completed for the following exercise, the error would be because the code is not what the program is expecting.

Use a Mixin to Add Common Behavior Between Unrelated Objects

Here is what the instructions ask us to do for this exercise.

Create a mixin named `glideMixin` that defines a method named `glide`. Then use the `glideMixin` to give both `bird` and `boat` the ability to glide.

Happy coding!

1 Like

@Gray.n.Grey is gives some great advice, but I suspect you like applying what you learn to something you relate to personally. I learn like that too.

You define two objects with the following code (for sake of argument let’s say they are unrelated).

let myFullName = {first: "Bushra", _$Second: "Abdulsalam", _Last: "Ghames"};

let myFullNameAgain = {first: "Bushra", _$Second: "Abdulsalam", _Last: "Ghames"};

A mixin is defined to give some functionality unrelated objects. So, now you have two unreated objects (e.g. myFullName & myFullNameAgain) and a mixin as defined below.

let LogMyFullNameAgain = function(obj) {
  obj.whatever = function() {
    return "Nothing";
  }
};

Specifically, the mixin gives the ‘whatever’ method to an object.

Then, in your final line of code, you give only the myFullNameAgain object the new functionality and print it to the console which correctly displays ‘Undefined’. ‘Undefined’ is the correct output since the method was never executed.

console.log(LogMyFullNameAgain(myFullNameAgain));

If you write:

LogMyFullNameAgain(myFullNameAgain);

console.log(myFullNameAgain.whatever());

You will get the expected return value from the whatever method.

"Nothing"

Does this help?
Keep up the good progress!

Happy Coding! :slightly_smiling_face:

1 Like