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!
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!
Share Improve this question asked Nov 2, 2020 at 18:40 GermánGermán 651 silver badge7 bronze badges4 Answers
Reset to default 5A 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)
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)
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);
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);