Being debuged with out knowing the issue make me angry :/


#1

var Animal = function (name , numLegs){
this.name=name;
this.numLegs = numLegs ;
}
// create the sayName method for Animal
Animal.prototype.sayName = function (){
console.log("Hi my name is" + " " + Animal[name]);
}
// provided code to test above constructor and method
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();


#2

Could you describe your problem in a little more detail, please?
I'd go with this statement:

console.log("Hi my name is" + " " + Animal[name]);
Animal[name]

for creating problems but as said more information would be useful.


#3

hey haxor . my friend the outputs is like this :
hy my name is undefined // instead of the animal name ;
i saw in the qa forum , they put this.name and it works .
but i do not understand what the difference between the instrucution of animal.name ; animal[name]
and this.name


#4

Actually I'm not 100% sure what is happening and still a little confused that the variable name name isn't already raising a Reference Error. First guess would be that you access some kind of a default property name of the window object which seems to be an empty string. So trying Animal[""] you don't get a reasonable property and therefore the value of undefined. Also keep in mind that in javaScript everything is object so what you're accessing by Animal[] are the properties of the constructor function not the object created by it. Whereas this. refers to the object containing it which could also be the object you create using the constructor.


#5

but my friend in the last exercices we used to call the name of an object by object.name
not this.name


#6

You said it: the name of an object but you're calling here the name of the constructor function (object). And you're using name in it's brackets but name was never defined inside of this function or globally, was it?


#7

yes indeed . i need more practice on objects instructions