Skip navigation

it turns out that there are 2 functions i didn’t know about all this time:

function sampleFunctionObject( p1, p2 )
    if( this.alert )
    // if you use sampleFunctionObject( 'value', 'value2' ); or
    // null, 'value', 'value2' );
    // then you will end up executing this function,
    // but the 'this' variable will be bound to the global object.
    // WRONG INVOKATION STYLE!  I don't want to clobber
    // the global object with the new properties that
    // this function will attach to 'this'
        alert( '"this" is the Window object: ' + this );
        return ;

    // Here you've made a call like

// obj, 'val1', 'val2' );

    // so the 'this' variable is a reference to obj, the first arg
    // to the call() method.
    this.property1 = p1;
    this.property2 = p2;

    this.print = function()
      document.write( '<p>' + p1 + ' ' + p2 + '</p>' );

var obj = { };  // make an empty object literal

// CALL sampleFunctionObject ON obj (basically,
// CALL sampleFunctionObject AS IF IT WERE
// to executing sampleFunctionObject, the FIRST
// parameter is going to be set as 'this' inside
// the body of that function.  The other following
// parameters get passed in order to the function's
// original parameter list ( p1, p2 ) obj, 'parameter 1', 'parameter 2' );

// the .apply() method is the same as .call(), except you pass
// the arguments inside an array instead of as separate
// arguments in a row.
sampleFunctionObject.apply( obj, ['paramt1', 'paramt2'] );

So this is what makes it possible to invoke a parent constructor assuming you’re using the pseudoclassical inheritance pattern as outlined by douglas crockford.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: