original source from http://9eye.net/entry/Scope-in-javascript-3
/**
* @author mulriver
*/
// utility method for any objects
function _check(msg){
this.value.println(msg);
}
// creating object 'obj'
var obj = {
value: 10,
check: _check // set utility method to member method
}
// define the constructor of 'func' function object
var func = function(){
this.value = 20; // define member variable 'value'
}
func.prototype.check1 = function(msg){
obj.check(msg); // using object 'obj' for member method (it's object composition technique)
}
func.prototype.check2 = obj.check; // set object 'obj'`s method to member method
var newFunc = new func(); // get a instance of object 'func'
newFunc.check1("indirect"); // using object 'obj' directly. => 10
newFunc.check2("direct"); // using method of object 'obj' => 20
newFunc.check2.call(obj, "call"); // using a one`s member method with other`s scope => 10
_check.apply(newFunc, ["apply"]); // using a utility method with scope => 20
최근 덧글