News for hackers. Stuff that matters.
Re:How Javascript Prototypal Inheritance Really works by richcollins on Saturday May 28, 2011 @04:48PM (#2595187)
This is faster (don't keep calling new Function) and more OO (send message to prototype to clone it):

    Proto = {
    	cloneConstructor: new Function,

    	clone: function()
    	{
    		Proto.cloneConstructor.prototype = this;
    		return new Proto.cloneConstructor;
    	}
    }

    var Point = Proto.clone();
    Point.x = 0;
    Point.y = 0;
    Point.print = function()
    {
    	console.log(this.x, this.y);
    }

    var p = Point.clone();
    p.x = 10;
    p.y = 20;
    p.print(); //10 20

There are no comments on this comment yet.