DEV Community

Learning JavaScript Design Patterns

yev on October 02, 2018

I started picking up JavaScript again months ago. I created a repo for some notes that I created while learning Design Patterns in the context of J...
Collapse
 
ycmjason profile image
YCM Jason

Had a quick look at the "Creational Design Patterns".

You put these three together:

var newObject = {};
var newObject = Object.create(null);
var newObject = new Object();

Which would naturally mislead the readers to think they are equivalent. You should note that Object.create(null) do not inherit the Object.prototype.

There is also an error in the code example:

const defineProp = function(obj, key, value) {
    config.value = value; // config is not defined???
    Object.defineProperty(obj, key, config);
}

defineProp(newObject, 'someKey', 'Hello World');
Collapse
 
vycoder profile image
yev

Hi, thanks for the feedback! I've corrected said code snippet. Thanks for pointing that out.

As for your first concern. They indeed have some differences but I figured to just write something about it as a different topic. I should've gone first with that but my intention was first and foremost, to learn design patterns for myself. Right now, I'm just addressing JavaScript caveats that has something to do with design patterns context within JavaScript. Like defining a function within the function constructor versus via the prototype.

Collapse
 
vikasnautiyal profile image
vikas nautiyal

Which book are you referring to? It would be great to have practical examples of different design patterns it greatly helps visualise the requirement and usage of these patterns. Best wishes!

Collapse
 
vycoder profile image
yev

Hi, I forgot to link the actual book I'm using, I've updated the post now. Thanks for your feedback. It's Learning JavaScript Design Patterns by Addy Osmani like Thomas said.

I'll try to make practical examples like that as much as possible especially on those patterns that really needs extra attention.

Collapse
 
thomasjunkos profile image
Thomas Junkツ

addyosmani.com/resources/essential...... just a wild guess 😉

Collapse
 
vycoder profile image
yev

Yes you're right, thanks! I forgot to add it on the post I was so sleepy the night I posted this. 😉