Assessment for object basics 4

here is my link to the codepen

Here is the link to the page learning page.


Is my object notation for creating cat2 right? plz suggest on this.

Unfortunately, that’s not correct.
You need to rewrite the two existing object to one function as described in the lesson here: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics#introducing_constructors

From the above page:
A better way is to use a constructor . A constructor is just a function called using the new keyword. When you call a constructor, it will:

  • create a new object
  • bind this to the new object, so you can refer to this in your constructor code
  • run the code in the constructor
  • return the new object.
    Constructors, by convention, start with a capital letter and are named for the type of object they create. So we could rewrite our example like this:
function Person(name) {
  this.name = name;
  this.introduceSelf = function() {
    console.log(`Hi! I'm ${this.name}.`);
  }
}

To call Person() as a constructor, we use new :

const salva = new Person('Salva');
salva.name;
salva.introduceSelf();

const frankie = new Person('Frankie');
frankie.name;
frankie.introduceSelf();

Please ask if you need more help.

Cheers,
Michael

Is this correct?

function Greeting(name, breed) {
this.name = name;
this.breed = breed;
this.greeting = function() {
console.log(Hello, said ${this.name} the ${this.breed})
}
}

const cat = {
name : ‘Bertie’,
breed : ‘Cymric’,
color : ‘white’,
}
}

const greet = new

const cat2 = {
name : ‘Elfie’,
breed : ‘Aphrodite Giant’,
color : ‘ginger’,
}
}

cat.Greeting();
cat2.Greeting();

Hi @Felipe_Vieira

This is not quite right. Your class should be called Cat() and not Greeting() and should have three parameters: function Cat(name, breed, color) { ... }.

Then you would call this class with the values for the cats. For example:
const cat = new Cat('Bertie', 'Cymric', 'white');

When you have created you two cat instances you can the call there greeting() method.

Please put your work into an online editor like CodePen. It’s much easier verify it for us. :slightly_smiling_face:

Have a nice day,
Michael

1 Like

function Cat(name, breed, color){
this.name= name;
this.breed=breed;
this.color=color;
this.greeting = function(){
console.log(Hello, said ${ this.name } the ${ this.breed }.)}}

const bertie = new Cat(‘Bertie’, ‘Cymric’ );
bertie.name;
bertie.breed;
bertie.greeting();

Hello @Arpita_Singh.
Sorry for the late reply.
There are two minor errors in your code that may stop it from running:

  1. In your Constructor where you created a method.

It should be this.greeting = function(){ console.log(`Hello, said ${this.name} the ${this.breed}.`)
Note: Remember to use backticks (``) when you want to use template literals.

  1. A second error which will prevent your code from running is the line:

. if you check it and compare it with the Constructor’s arguement

, you will notice you are missing an arguement (which is the color arguement)when you want to create a new object using the Constructor. So to remove this error you can add the last arguement by changing it to const bertie = new Cat('Bertie', 'Cymric', 'white' );

So that is all my fellow dev. Feel free to reach out if you need help.
Thank you.