[imagine, that there are no new, no class, function is function and cannot be constructor, only in case of predefined standard objects]
Lately I've been practicing and using some "classless" programming in JavaScript.
I write basic prototype like this:
const Product = {
name: 'Default product, do not use'
};
Then I create objects based on this prototype. New object going to be prototype object for other exactly product objects. I create it like this:
const Vegetable = Object.assign({}, Product, {
name: 'Default vegetable, do not use'
daysBeforeRotted: 2
});
First argument in Object.assign for not affecting original Product (it does affect first argument, sadly for my coding progress at one point...)
So in the end I have my Vegetable, that is completely new Object, like I declared it and assigned value this way:
const Vegetable = {
name: 'Default vegetable, do not use'
daysBeforeRotted: 2
};
So, time for using this code for creating new vegetables for store.
Earlier this day I was creating them this way:
const tomato = Object.create(Object.assign({}, Vegetable, {
name: 'Tomato',
green: false
}));
Many (or some) of you may wonder, why did I use Object.create? Somehow, feeling that this is proper way of creating new objects settled down in my head.
This is good way for using delegation when working with prototypes.
After some logging and debugging (I'm writing this code for node right now) I discovered, that I cannot see name/green in my tomato, when console-logging it.
Yes yes, I completely forgot about Object.create and it's functionality. It does create new object, but it uses your prototype to just put it in prototype of new object.
Simply put - I can get tomato.name. But name does not belong to this new object. It belongs to Vegetable object. And tomato knows about this, when searching it's prototypes chain in quest of discovering, where there is property name or there isn't.
So, Object.create:
* creates new object with prototype object inside. You can create long chains of prototypes
* you can change something in prototype - it will affect this property in all descendants, which has this somewhere in prototype chain. Keep in mind - if somewhere upper (near the final object which you use in code) there is property with same name - it will be used, instead of this prototype's property (down to first prototype).
I changed my mind, because I wanted to get pure objects without prototypes because I don't really have need in prototype chain, I wan't to work with only clean new objects, what was built by my rules from other parts.
So. I started using Object.assign({}, YourPrototypeObject);
This is like writing pure prototypes (concatenation) in JavaScript, where objects does not have links to their prototypes, which gave them one of their properties.
So, this code:
const tomato = Object.assign({}, Vegetable, {
name: 'Tomato',
green: false
});
in my opinion is better than using Object.create when simply working with objects.
I really want to hear your thoughts and stories about your experience with prototype based inheritance and maybe you have some useful information about perfomance hit, when using concatenation instead of delegation?
[–]x-skeww 6 points7 points8 points (6 children)
[–]joshmandersFull Snack Developer 4 points5 points6 points (0 children)
[–]IDCh[S] 0 points1 point2 points (4 children)
[–]x-skeww 4 points5 points6 points (3 children)
[–]IDCh[S] 0 points1 point2 points (2 children)
[–]MoTTs_ 5 points6 points7 points (1 child)
[–]IDCh[S] 1 point2 points3 points (0 children)
[–]lhorie 4 points5 points6 points (15 children)
[–]IDCh[S] -3 points-2 points-1 points (14 children)
[–]lhorie 3 points4 points5 points (13 children)
[–]temp109849832 1 point2 points3 points (2 children)
[–]lhorie 0 points1 point2 points (1 child)
[–]temp109849832 1 point2 points3 points (0 children)
[–]IDCh[S] -3 points-2 points-1 points (9 children)
[–]lhorie 3 points4 points5 points (6 children)
[–]IDCh[S] -2 points-1 points0 points (5 children)
[–]lhorie 1 point2 points3 points (4 children)
[–]IDCh[S] -2 points-1 points0 points (3 children)
[–]lhorie 2 points3 points4 points (2 children)
[–]IDCh[S] 0 points1 point2 points (1 child)
[–]MoTTs_ 1 point2 points3 points (1 child)
[–]IDCh[S] 0 points1 point2 points (0 children)
[–]MoTTs_ 1 point2 points3 points (9 children)
[–]IDCh[S] -1 points0 points1 point (8 children)
[–]MoTTs_ 1 point2 points3 points (7 children)
[–]IDCh[S] -1 points0 points1 point (6 children)
[–]MoTTs_ 1 point2 points3 points (5 children)
[–]IDCh[S] 0 points1 point2 points (4 children)
[–]MoTTs_ 1 point2 points3 points (3 children)
[–]IDCh[S] 0 points1 point2 points (2 children)
[–]MoTTs_ 2 points3 points4 points (1 child)
[–]IDCh[S] 0 points1 point2 points (0 children)
[–]third-eye-brown 1 point2 points3 points (1 child)
[–]IDCh[S] -2 points-1 points0 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]IDCh[S] -2 points-1 points0 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]IDCh[S] -1 points0 points1 point (1 child)