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
// sampleFunctionObject.call( 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
// sampleFunctionObject.call( 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
// A MEMBER FUNCTION OF obj. WHEN we get
// 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 )
sampleFunctionObject.call( obj, 'parameter 1', 'parameter 2' );
obj.print();
// 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'] );
obj.print();
- quickref .apply()
- quickref .call()
- wiki.laptop on .call();
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.