.call()

by WilsonPage

HTML

<script src="https://raw.github.com/gist/1438757/abb1656baa6a236d4609f7c1f4729b091f4f611c/print_r.js"></script>

JavaScript

// dummy function
var myFunction = function(customParam1, customParam2) {
    alert(customParam1);
    alert(customParam2);
    alert(this.scopeValue1);
    alert(this.scopeValue2);
    
    // print the scope (using my custom method)
    $.print_r(this);
};


// a dummy custom scope
// whatever is passed into the first argument
// of .apply() become the value of 'this'
var customScope = {
    scopeValue1: 'this is value 1 in custom scope',
    scopeValue2: 'this is value 2 in custom scope',
};


// run using .apply()
myFunction.call(customScope, "this is custom param 1", "this is custom Param 2");