Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 1.01 KB

File metadata and controls

31 lines (23 loc) · 1.01 KB

WeakMaps in ES6 Harmony

When WeapMaps land in the upcoming ES6 Harmony proposal private member in JavaScript will be simple to implement:

var Person = (function () {
    var privates = new WeakMap();

    function Person(name, age) {
        // Create new privates
        privates.set(this, {
            name: name,
            age: age
        });
    }

    Person.prototype.name = function () {
        return privates.get(this).name;
    };

    Person.prototype.sameAge = function (other) {
        return privates.get(this).age === privates.get(other).age;
    };

    return Person;
}).call();

This is because the new Map object can take objects as keys. WeakMap builds on this by not holding strong references to keys or values, allowing them both to be GC'd naturally.

You can already try this in Node with node --harmony, FireFox >=6.0 or Chrome switching on Enable Experimental JavaScript at chrome://flags.