Category Archives: javascript

Dynamic Getter

Getters in ES6 are usually defined within a class like this:

  class RandomPoint {
    get x() {
      return Math.random();
    get y() {
      return Math.random();

In this case we use getters to make a random point:

  let pnt = new RandomPoint();
  console.log(pnt.x, pnt.y);
  // outputs something like 0.09403673512366306 0.2717692041118973

I found myself wanting to be able to dynamically define a getter. This can be done like this:

let rand = function() { return Math.random(); };
Object.defineProperties(someObj, { ['x']: { get: rand });

This is really all just sugar, but still fun stuff…

Also posted in Uncategorized | Tagged , | Leave a comment

HTML5 Canvas Tutorial

If you haven’t tried the html5 canvas tag yet, I suggest you give it a try. It falls under the same category as ActionScript’s Graphics and BitmapData classes. It is however much much simpler. One great thing about it is it runs really fast on IOS5. Have a look at some of these demos to see what I mean:

A few days ago I recorded this short video tutorial… it’s very simple, but it will get you started if you’ve never tried canvas before:

Also posted in html5 | Tagged , , , , | 2 Comments