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.
here is my link to the codepen
Here is the link to the page learning page.
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:
this
to the new object, so you can refer to this
in your constructor codefunction 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();
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.
Have a nice day,
Michael
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:
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.
. 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.