Giving Object Context to Function Calls

Javascript has a dynamic object named 'this'. By default 'this' equals to the global object container, so if we have a global variable named x, then this.x === x. The code below will display the value of x, which is 10.
var x = 10;
var print_x = function () {
    alert (this.x);
};
print_x();
For those who are familiar with OOP (especially Java) must be very familiar with the 'this' object. Accessing this.x in a method means accessing attribute x of the object containing this method. We can use this OOP feature in by assigning javascript function to an object attribute. The code below will display a value of 20.
var print_x = function () {
    alert(this.x);
};
var y = {'x': 20, print_x: print_x};
y.print_x();
You can seen that the value of 'this' in function print_x is changed depending on where it was assigned to. The code below will display a value of 30.
var z = {'x': 30, print_x: print_x};
z.print_x ();
If desired so that the value of 'this' does not change wherever print_x function is assigned to, other techniques are required.
Bind function below is taken from the prototype.js library:
Function.prototype.bind = function () (
    var fn = this, args = Array.prototype.slice.call (arguments), object = args.shift ();
    return function () (
        fn.apply return (object, args.concat (Array.prototype.slice.call (arguments)));
    );
);
Now the code below will still display the value of 20 despite what being called is z.print_x:
var y = {'x': 20};
print_x = print_x.bind (y);
var z = {'x': 30, print_x: print_x};
z.print_x ();