Naman Goel

talk about code

Page 3

Object getters and setters. And it’s many gotchas

In my previous post, I talked about how you could use Object.create() (or Object.defineProperty()) to supercharge your javascript object with unwritable properties. In this post, I’ll talk about what else is possible with Objects in ES 6.

Firstly, I would like to put it out there, that just as ‘write’ can take a boolean value for an object keys, there are are ‘read’ (does what it says) and ‘configure’ (that enables and disables further behaviour modification) properties as well.

But what can be even more exciting that setting read-only values on objects? It’s getters and setters. Many other languages have had them for objects, and javascript is finally getting it’s native implementation. I would love to tell you that everything is great in javascript land, but that would be a lie. On the surface, getters and setters seem to be a great idea, but once you actually look at them, they are...

Continue reading →

Creating Magic with Object.create().

Javascript’s approach to objects can be quite daunting. There are a few ways to achieve the goal of making an object factory. In general, though, it comes down to two ways.

1: Factory Function

var makeObject = function(){
  var obj = {};
  obj.prop = "object name";
  return obj;

This is probably the most explicit way of creating an object constructor or the closest thing to a class in a javascript. (The Pseudo-classical way is another but we’re not talking about that here.)

The interesting part comes when you want to add a prototype to this object. The code turns to something like this:

var makeObject = function(){
  var obj = Object.create(prototype); //pass in the prototype object
  obj.prop = "object name";
  return obj;

(If you don’t know about prototypes, you should read about that here. The interesting part is the optional, second argument of Object.create. Head over to...

Continue reading →

Using Handlebars.js with Sails.js

Sails.js is a great framework that is easy to learn, well-documented and getting a lot of traction in the market. Sails.js is based on Ruby on Rails and follows its philosophy of convention over configuration. There is a lot of magic and things just work. That said, the magic seems to make more sense here than in Rails, and it usually doesn’t surprise you. (That’s a good thing.)

The problem is that with convention over configuration philosophy comes the assumption that most people will stick to defaults. As a result, straying from the expected defaults can land you in a lot unchartered territory with little community help and almost no documentation.

In my recent project, Scribbler I strayed from the conventional Sails.js path on multiple occasions and after much strife, I emerged victorious. Here’s my story.

Easy beginnings

Right off the bat, Sails is super easy to work with, as is...

Continue reading →

Why Gulp.js is awesome, and how to use it with Sails.js

A few months ago, the folks over at Fractal, decided to reinvent Grunt. After identifying and fixing the problems with Grunt, they ended up with Gulp.js

And how do you think the internet responded? You’re right, it immediately turned into a flame war. No one wanted to enjoy the fact there was now more choice. Immediately people started parsing Gulp or rejecting it outright.

I’m not going to spoil all the fun. When, lives are not at stake, wars can be fun! I’m here to praise Gulp and tell you why you should probably switch.

Let me start with what isn’t great about Gulp. It can all be attributed to its newness –

  • It doesn’t have as many plug-ins
  • It doesn’t have as much documentation
  • It’s not integrated into popular tools and frameworks like Grunt
  • It’s probably hard for people who don’t know any javascript, (but Grunt isn’t any easier)

On the other hand, there are a bunch of things...

Continue reading →