My understanding was that all classes were essentially functions, and all instances were essentially objects. But something confused me.
//Take this for example:
function AnimalFunc(name) {
this.name = name;
this.sayName = function() {
console.log(this.name);
}
}
//And compare it with this:
class AnimalClass {
constructor(name) {
this.name = name;
}
sayName() {
console.log(this.name);
}
}
//Now I instantiate them.
cat = new AnimalFunc("cat")
cat.sayName() // -> "cat"
dog = new AnimalClass("dog")
dog.sayName() // -> "dog"
console.log(Object.keys(cat));
console.log(Object.keys(dog));
Expected Observations:
typeof(AnimalClass)
and typeof(AnimalFunc)
both return function
.typeof(cat)
and typeof(dog)
both return object
.Unexpected Observations:
Object.keys(cat)
, I get ["name","sayname"]
.Object.keys(dog)
, I get ["name"]
.My question is: why do I not get sayname
as a key for the class instance? Why do I get it only for the function instance?
The first example, with a function, creates a new function property when creating an instance, and therefore, a new key for Object.keys
to pick up.
The second example, with a class, assigns the function property to the object's prototype
instead. Because of this, a new key isn't actually created, and the sayName
function will be found by walking up the prototype chain.
You can replicate the class behavior by doing this:
function AnimalFunc(name) {
this.name = name
}
AnimalFunc.prototype.sayName = function () {
console.log(this.name)
}
Here's some reading if you want to familiarize yourself with JS's prototype model. Can find more articles and such by searching up "javascript prototype", if you like.
Because you have declared two properties named name
and sayname
this.name;
this.sayname;
In the class AnimalClass
you only declared as property the key name
within the constructor body:
this.name = ....
Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.
So, basically will return the properties directly declared using the context this
.
class AnimalClass {
constructor(name) {
this.name = name;
// This is just to illustrate!
this.sayName = function() {
console.log(this.name);
}
}
}
console.log(Object.keys(new AnimalClass('Dog')))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With