Home

Awesome

chronometer.js

This is just a chronometer constructor in JavaScript, that uses ObjectEventTarget as prototype to support events listeners.

DEMO PAGE

Motivation

I want to create a demo how to use the ObjectEventTarget and inspire other JavaScript programmers to use prototype.

Nothing better then a project that evolves a logical part that can be totally separated from the design, and also have it owns events. This will allow you do a lot of things, so you also can prototype your function with the chronometer, creating a more sophisticate object.

How to use

You just need to create a instance or more using new Chronometer(), then you are good to go.

NPM Install

npm install chronometer.js

It works fine in Nodejs and with Browserify, you can do a var Chronometer = require('chronometer.js'); and you are ready to go.

Methods

Public methods:

Internal use methods:

Properties

** Read-write properties:**

** Read-only properties:

Internal use properties:

Events

Events are provided by ObjectEventTarget prototype, means that you can use addEventListener, removeEventListener or dispatchEvent in any instance of chronometer.

Cancelable Events:

Non-cancelable Events:

Constants

Prototype

To prototype you can call from your new constructor passing your context.

Example:

function FancyChronometer( name ){
    Chronometer.call(this);
    this.name = name;
}
FancyChronometer.prototype = Chronometer.prototype;

Architecture

There is no private methods, except by the instance ChronometerStep that only can be accessed by createStep. The thing is, you have freedom to do whatever you want, even destroy the chronometer.

Object.defineProperty isn't been used to protect the read only properties, because I want to be backward compatible with ES3 and because it's more about good sense to don't change it values, but if you need for any misterious reason, you can change it.

autoUpdateID is an common example of how prototypes mean to be used if you want to give abbility to prototype any new constructor. If you want to hide it value from the other programmers, the only way is to use a WeakMap for that, and keep the methods that depends on it in the prototype, or create the method autoUpdate with a closure inside the constructor, but it's more code, memory mess just to protect other programmers from doing mistake with it. Doesn't worth.

If your browser has support to Object.defineProperties, it will add all methods as not enumerable, if you do a for in, they will not be iterated. But if you plan to give support to IE-9, use hasOwnProperty or a shim to ensure that behaviour.

Files