, sayInternal . , sayInternal, . .
this , . func(), this ( window ). obj.func(), this obj.
"" :
var method = obj.func;
method();
this . JavaScript , , .
call apply:
var MyComponent = function(params)
{
setup.call(this, params);
this.doSomething()
{
};
function setup(params)
{
var _this = this;
$(".some-element").click(function(){
_this.doSomething();
});
}
};
:
var Item = function()
{
this.say = function()
{
alert("hello");
};
this.sayInternal = function()
{
_sayInternal.call(this);
};
function _sayInternal()
{
this.say();
};
};
, , this.sayInternal. , Item , , .
:
var Item = function() {
};
Item.prototype = (function() {
function _sayInternal() {
this.say();
};
return {
say: function() {
alert("hello");
},
sayInternal: function(){
_sayInternal.call(this);
}
}
}());
, _sayInternal , () , say sayInternal . "" _sayInternal say sayInternal.