Arto8 PHP


Functional inheritance

arto8('', 'arto8.extend', function(extend) {
    function foo(value) {
        console.log('original foo, value = ' + value);
        return value + 1;
    }
    
    foo = extend(foo, function(value) {
        console.log('before, value = ' + value);
        //  call base function
        var rv = extend.base(value);    // extend.base(); - alernative variant
        console.log('after, value = ' + value);
        return rv;
    });
    console.log(foo(8));
    
    var object = {
        foo: function(value) {
            console.log('original foo, value = ' + value);
            return value + foo(2);
        }
    };
    
    extend(object, {
        foo: function(value) {
            console.log('object before, value = ' + value);
            //  call base function
            var rv = extend.base();    //  extend.base(value); - alternative variant
            console.log('object after, value = ' + value);
            return rv;
        }
    });
    console.log(object.foo(7));
});            
before, value = 8
original foo, value = 8
after, value = 8
9
object before, value = 7
original foo, value = 7
before, value = 2
original foo, value = 2
after, value = 2
object after, value = 7
10