Comments (14)
look! I find one:
class Pet {
constructor(sound) {
this.sound = sound;
}
}
class Dog extends Pet {
constructor (sound){
super(sound);
}
}
const aDog = new Dog('au') ;
aDog instanceof Dog ; //-> true
aDog instanceof Pet ; //-> true
Object.getPrototypeOf( Dog ) ; //-> class Pet
const aPet = new (Object.getPrototypeOf( Dog ))
aPet instanceof Dog ; //-> false
aPet instanceof Pet ; //-> true
from dmitripavlutin.com-comments.
Great post as always.
from dmitripavlutin.com-comments.
Comment written by Felix Sanz on 01/16/2020 17:37:32
I don't understand the example in section 5.2. Can't you just do if (this.name === '') {
?? If the property is inherited, why calling super on the method?
from dmitripavlutin.com-comments.
Comment written by Dmitri Pavlutin on 01/16/2020 19:29:56
The chapter explains the method overriding.
from dmitripavlutin.com-comments.
Comment written by Felix Sanz on 01/16/2020 20:31:54
But your example is not overriding nothing at all because the inherited method is so dummy, right? It would make more sense if the parent method contains some code, not just return the property (because the property was accesible without calling super on the method anyway). I'm right?
from dmitripavlutin.com-comments.
Comment written by Dmitri Pavlutin on 01/16/2020 20:45:04
The method overriding enhances the parent method (simple or not it does not matter) by providing a default value if the returned value is an empty string.
Thatβs a simple demo, donβt expect enterprise level code. π
Anyways, if you know a better sample, feel free to create a pull request for the post.
from dmitripavlutin.com-comments.
Comment written by igor thetigor on 02/17/2020 23:56:37
Dmitri, thank you so much for all your posts. Really good work, absolutely worthwhile. props!
from dmitripavlutin.com-comments.
Comment written by Dmitri Pavlutin on 02/18/2020 08:36:03
Thanks!
from dmitripavlutin.com-comments.
Comment written by Nayaka Yoga Pradipta on 07/29/2020 13:54:40
Thank you so much. Your article is easy to understand!
But do you have reference small project example that uses JS class?
from dmitripavlutin.com-comments.
Comment written by Dagg on 10/14/2020 21:20:39
Class is actually a function, it is a syntactic sugar for a function constructor. And function constructor is a way to generate objects.
Prototypal inheritance is something different, it creates object literals via Object.create() from another object that acts as a prototype of a newly created object. So class is not generated via prototypal inheritance. Thanks
from dmitripavlutin.com-comments.
So, how should a class method be added after the class declaration?
from dmitripavlutin.com-comments.
Thanks for this great article. I miss two methods in this guide that may not yet exist in JavaScript. First a method that answers how many subclasses a class has. Second, the opposite: a method that reports what the superclass is.
Example:
class Foo(object): pass
class Bar(Foo): pass
class Baz(Foo): pass
class Bing(Bar): pass
print([cls.__name__ for cls in Foo.__subclasses__()])
# ['Bar', 'Baz']
for cls in Foo.__subclasses__():
print(cls.__base__)
# <class '__main__.Foo'>
# <class '__main__.Foo'>
from dmitripavlutin.com-comments.
Thanks for this great article.
@meetsCode You're welcome! π
I miss two methods in this guide that may not yet exist in JavaScript. First a method that answers how many subclasses a class has. Second, the opposite: a method that reports what the superclass is.
This can be useful, however, I'd rather not include these into the post.
from dmitripavlutin.com-comments.
Hi, can you please provide cases where private fields are useful?
The second question, if I create a page object model (not on Cypress) does it make sense to use private fields to protect selectors?
from dmitripavlutin.com-comments.
Related Issues (20)
- react-usememo-hook/ HOT 12
- javascript-json-modules/ HOT 7
- javascript-import-meta/ HOT 3
- javascript-array-group/ HOT 16
- javascript-array-flatmap/ HOT 23
- interface-vs-implementation/ HOT 11
- vue-next-tick/ HOT 4
- props-destructure-vue-composition/ HOT 7
- ref-reactive-differences-vue/ HOT 5
- environment-variables-javascript/ HOT 3
- ref-in-vue/ HOT 1
- typescript-function-type/ HOT 5
- vue-v-model-form-inputs/ HOT 2
- react-forwardref/ HOT 4
- javascript-enum/ HOT 19
- react-comments/ HOT 8
- typescript-record/ HOT 10
- screen-window-page-sizes/ HOT 3
- javascript-pure-function/ HOT 1
- Great article
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dmitripavlutin.com-comments.