Ian Bicking: the old part of his blog

Re: Prototype.js and Object.prototype

Did you get the email I BCC'ed you on with the URL to the MochiKit svn repo?

Bound functions can be done there, manually, with bind(func, self), or in the constructor with bindMethods(this) to iterate over the properties in this and turn all functions into bound ones..

I'm working on an object protocol that's something like Python classes, but I haven't yet decided on what good semantics are like for JavaScript.

Comment on Prototype.js and Object.prototype
by Bob Ippolito

Comments:

I did see that. In this case I was just trying to reform Prototype, since I'd already started using it. I remember trying this:

def extend(ob1, ob2) {
    for (property in ob2) {
        if (typeof ob2[property] == 'function') {
            ob1[property] = ob2[property].bind(ob1);
        } else {
            ob1[property] = ob2[property];
        }
    }
    return ob1;
}

And it didn't work. Prototype's Function.prototype.bind() is, I assume, like MochiKit's bind(). I don't know quite why it didn't work -- maybe because by binding to a specific object you defeat further inheritance.

# Ian Bicking

You do defeat further inheritance like that.. however, in theory, you should be able to do it like this (which is one of the things I'm considering for the object model):

function isBoundFunction(func) {
    return (typeof(func.im_func) == 'function');
}

function bind(func, self) {
    var im_func = null;
    if (isBoundFunction(func)) {
        im_func = func.im_func;
    } else {
        im_func = func;
    }
    func = function () {
        return func.im_func.apply(func.im_self, arguments);
    }
    func.im_func = im_func;
    func.im_self = self;
}
# Bob Ippolito

With return func; at the end, of course.

# Bob Ippolito

Ok, just tested it.. it works in practice. Check the new implementation in svn (which is very similar to this, but it has tests and stuff).

# Bob Ippolito