Counting people on stairs – or IoT with a particle photon and node.js

In this article I will show you in 3 easy steps how to actually get started with an IoT a project build with a particle photon and a node.js server in order to have your own dashboard. I admit, IoT is a bit of a trend these days, and yes I jumped on the bandwaggon too. But since visiting the maker faire Zürich I have seen so many enthousiastic people building things, it has also motived me to also try out something. Thats why I decided to count the people that are running up and down our stairs at Liip. Follow along if you are – like me – a total noob when it comes to connecting wires but still want to experience the fun of building IoT devices.


Continue reading about Counting people on stairs – or IoT with a particle photon and node.js

Tags: , , , , ,

Why I don’t use the javascript “new” keyword

Coming from the PHP world, I’ve spent a lot of time trying to reproduce or finding a similar way to work with objects and inheritance with Javascript. A lot of libraries gives you their own style of a class-like functionality with javascript, and yes, it will work similarly as OO classes, but after a big javascript project, you will probably want to understand what’s all this prototype stuff appearing in your console during your debug sessions, and eventually find out that the classical inheritance and its preferred keyword new does not suit javascript so well.

forget about new

The first thing that confuses developer that really start developing with javascript is the new keyword. “ah ok, like in PHP or Java, I can create instances of Car with new Car(), easy”. I won’t dive into [history details] here, but you have to know that new was only introduced to let javascript gain more popularity. new is syntactic sugar, but hides the real prototypal nature of the language.
Check the following constructor pattern in javascript:

It looks not so bad. Well I have to use this prototype keyword there, but ok, if it works like that. Let’s define a square now, which is kind of a rectangle, so it extends our Rectangle class:

Isn’t that much clearer? Object.create is a native javascript function in ECMAScript 5, compatible in all major browsers (there is a polyfill for old browsers). The argument it takes is the object we want to extend (it can take a second one but I won’t talk about it in this post). The way to define width and height is not so nice though, let’s create kind of a constructor:

create is kind of a factory (avoid the term constructor now), and what’s nice is that it’s integrated in the object itself.
Let’s create the square now:

This is so more intuitiv: Square is a base object, once defined you don’t want to touch it (except if you want to modify all square instances). Then you create an instance of it by calling Square.create. Note that we didn’t have to use the prototype keyword to achieve inheritance.

What’s behind

Let’s have a look at the sq object in the console:

Details of a square instance

We can see that the properties of Rectangle are in __proto__, and width and height are own properties of the object. If the object doesn’t redefine its properties, the javascript engine will look for them in __proto__, and if it doesn’t find them, will dive deeper into __proto__ of __proto__, until it finds it (or an exception is thrown). So by nature, javascript has this inheritance chaining built-in, and there lies all the power of the language.

By the way you maybe also noticed that the very last __proto__ contains some basic functions: all objects in javascript inherit from Object and therefore have these native functions like valueOf or toString.

Beware of references

Because the objects extend other objects, you have to keep in mind that some properties of your object are in __proto__, and therefore were not copied when you created your instance. This can lead to unexpected behaviour if you forget about it, let’s see an example:

poorCar has 2 options too because both instances have their options property pointing to the base Car.options property. To fix it, you have to create an options array whenever you create a car instance. Let’s add a create function to do this:

Having properties in common can be very useful though, if I change the type property of Car, all the instances will get the new type.


Javascript requires another way of thinking, where Objects are first-citizen and can construct or be constructed easily, without the need for a new keyword.

Create a base object, extend from it, extend from others, change its behaviour, the language let you do nearly everything, leading to a lot of flexibility, but also to multiple way of doing things (which is not always good for developer, often looking for the right way to do what they intend to do).

Of course new has sometimes legitimate uses, and does not really cause problems if you don’t need inheritance. But are you really wanting to use it now that you read this?

Tags: ,