Re: Class prototype readonly

20 views
Skip to first unread message

Nicholas Zakas

unread,
May 1, 2020, 11:00:29 AM5/1/20
to zakas...@googlegroups.com
Nicholas, not Nic. :)

The prototype property cannot be overwritten, so PersonClass.prototype cannot be set to a different object. The prototype can still be modified.

for-in iterates over both own and prototype properties that are enumerable. getOwnPropertyNames returns only own properties but returns both enumerable and non enumerable properties. 

On Wed, Apr 29, 2020 at 8:20 PM Jeff <zhenzh...@gmail.com> wrote:
Hi Nic,

1st question, On page 167, the only note says the prototype of a class is readonly, but I tried with following code, it works well,

class PersonClass {
constructor(name) {
this.name = name;
}

sayName() {
console.log(this.name);
}
}

/* method or property could be assigned on the prototype
same as declaring class with function */
PersonClass.prototype.anotherFunc = function () {
console.log(this, 'another func');
};
PersonClass.prototype.age = 9;

let person = new PersonClass('jeff');

/* special note: for-in and getOwnPropertyNames are diff
for-in only gets props on prototype, getOwnPropertyNames
only gets instance attr */
for (const key in person) {
console.log(key);
}
console.log(Object.getOwnPropertyNames(person));


2nd question, as my comments show, for-in only gets all the props on prototype of a class, and Object.getOwnPropertyNames only gets attrs on the instance. I am asking that cuz you probably know it and I couldn't find a good answer for that online. 

thx & brs,
Jeff

--
You received this message because you are subscribed to the Google Groups "Zakas Books" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zakasbooks+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/zakasbooks/3d4271a6-f709-4a7b-a30d-d85844ab811c%40googlegroups.com.

Jeff

unread,
May 13, 2020, 4:25:36 AM5/13/20
to Zakas Books
Thanks Nicolas :-)
To unsubscribe from this group and stop receiving emails from it, send an email to zakas...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages