how to update object properties under the prototype 'augmentation' approach?

9 views
Skip to first unread message

Gallagher Polyn

unread,
May 28, 2015, 8:44:29 PM5/28/15
to nod...@googlegroups.com
Hey all,

How do I transfer properties, for example pushing to an array my receiverClass, while it is augmented by a 'givingClass' under Addy Osmani's augmentation approach?

I seem only able to effect the transfer of prototype functions, but can't monkey around with other property members or set properties on the receivingClass :(

// Extend an existing object with a method from another
function augment( receivingClass, givingClass ) {
 
    // only provide certain methods
    if ( arguments[2] ) {
        for ( var i = 2, len = arguments.length; i < len; i++ ) {
            receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]];
        }
    }
    // provide all methods
    else {
        for ( var methodName in givingClass.prototype ) {
 
            // check to make sure the receiving class doesn't
            // have a method of the same name as the one currently
            // being processed
            if ( !Object.hasOwnProperty.call(receivingClass.prototype, methodName) ) {
                receivingClass.prototype[methodName] = givingClass.prototype[methodName];
            }
 
            // Alternatively (check prototype chain as well):
            // if ( !receivingClass.prototype[methodName] ) {
            // receivingClass.prototype[methodName] = givingClass.prototype[methodName];
            // }
        }
    }
}





Reply all
Reply to author
Forward
0 new messages