Assessment Wanted for Object Basics Task 4

So when I put this code into the Mozilla Console, I get the “Uncaught SyntaxError: redeclaration of const cat”. I’m just not understanding that its being redeclared when it was only declared once and then called?

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

const cat = new Cat(“bertie”,“cymric”,“white”);

cat.greeting;

The error “Uncaught SyntaxError: redeclaration of const cat” suggests that you have previously declared a constant named cat in the same scope, and you’re trying to declare it again. In JavaScript, const declarations are block-scoped and cannot be redeclared in the same scope.

To fix this issue, you have a few options:

  1. Rename the cat variable: If you need to create a new Cat instance, use a different name for the variable.

    const anotherCat = new Cat("bertie", "cymric", "white");
    anotherCat.greeting();
    
  2. Run the code in a new console session: If you’re using a browser’s console, refresh the page or open a new console tab to clear the previous declarations.

  3. Change const to let or var: If you need to reassign the cat variable, use let or var instead of const. However, this is generally not recommended as it can lead to less predictable code.

Also, your greeting function declaration within the Cat constructor is not correct. It should be defined as a method of the object being created. Here’s the corrected version:

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.color} ${this.breed}`);
  };
};

const cat = new Cat("bertie", "cymric", "white");
cat.greeting(); // Note: calling the method with ()

Make sure to call cat.greeting() with parentheses to actually execute the function.

Ok now I just feel silly hah thank you!