0

Im doing a course in JavaScript. Im at the "Objects and classes" chapter, and I do not know how to solve some assignements in my homework. The first exercise is like this

function createCat(name,age){
//Create a new object with the property "name" and the value defined by the argument "name".
//Add a new property to the object with the name "age" and use the value defined by the argument"age"
//Add a methos (function) called meow that returns the string "Meow"!
}

And this is what im trying

 function createCat(name,age){
      var Cat={};
        Cat.Name=name;
        Cat.Age=age;
        Cat.meow=function(){return "Meow!"};
        return Cat;
     }

Im testing the function loading the script in an index.html file, opening that file in a browser and then testing the functions in the web Console. I run the function and there is no problem. Then, I test if the Cat object was returned by writing Cat.Name in the console, which results in an error. The same thing happens when I call the function in a line of code below and then try to access the properties of the Object. The error reads "ReferenceError: Cat is not defined". What am I doing wrong? Thanks!

4 Answers 4

5

A cleaner way to do this is to leave out the let Cat = {} part entirely. You can use the function itself to create a Cat object.

function Cat(name, age) {
    this.name = name;
    this.age = age;
    this.meow = () => console.log("Meow!");
}

let myCat = new Cat("Waldorf", 16)
let anotherCat = new Cat("Statler", 12)

myCat.meow()
console.log(anotherCat.name)
Sign up to request clarification or add additional context in comments.

1 Comment

I agree this is a better answer.
2

Your function returns Cat, but this is only a name in function scope. In order to use that name out of the function, you need to do this:

function createCat(name, age) {
        var cat = {};
        cat.Name = name;
        cat.Age = age;
        cat.meow = () => "Meow!";
        return cat;
}

let Cat = createCat("mist", 16);

console.log(Cat)

Comments

1

You get this error because Cat is only defined within the scope of your function. To define Cat globally, use window.Cat instead of var Cat:

function createCat(name, age) {
  window.Cat = {};
  Cat.Name = name;
  Cat.Age = age;
  Cat.meow = function() {
    return "Meow!"
  };
  return Cat;
}

console.log(Cat.Name);

3 Comments

This answer is actually not correct, because now the function will overwrite the window.Cat every time the function is called...
This is the only way you would be able to call Cat outside of the function. It is not a requirement (as per the exercise requirements) but it is the main reason the OP is getting the error.
The OP's function returns a cat, so it makes more sense to store that returned cat in a variable. For example: let cat = createCat("Jonesy", 10). That way the cat is available in the global scope AND you can have more than one different cat.
1

If you want to get your Cat when you type on the console Cat.name you have to declare it globally like so:

function createCat(name, age) {
  return {
    name: name,
    age: age,
    meow: function() {
      return "Meow!"
    },
  };
}

window.Cat = createCat('name', 2);

Then you can access your Cat globally.

You could also assign the Cat to a variable on the browser console and access it through Cat.name like so:

const Cat = createCat('name', 2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.