Reputation: 1569
Alright, given the following Javascript
code that I DO NOT WANT to modify:
(function () {
function iWantToCallThis(){
// Do some stuff
}
window.SomeObject = {
theirfunc = function(){
// Do some stuff
},
otherFuncIDontWantToCall = function(){
// This works, but don't want to call this function.
iWantToCallThis();
// does other stuff
}
}
}());
How can I access iWantToCallThis()
through the scope of SomeObject like so:
window.SomeObject.theirfunc = (function (func){
iWantToCallThis();
func.apply(this, arguments);
} (win.SomeObject.theirfunc));
Even though I would consider that function to -technically- run in its original scope, I do not have access to iWantToCallThis()
. Is there any way to access that function without editing the original source code?
Thank you!
Upvotes: 1
Views: 667
Reputation: 18078
Matthew,
IMHO, Douglas Crockford's "Private Members in JavaScript" is the definitive article on this topic.
It should help persuade you that Private members are externally inaccessible except via Privileged methods.
Upvotes: 1
Reputation: 9399
The short answer is that you can't.
The long answer is that if you change your mind about not changing the code, and then return iWantToCallThis
, you may end up making a closure. If you do it a lot you might have performance issues.
Upvotes: 1
Reputation: 21830
no, because iWantToCallThis
is neither returned from the IIFE nor is it stored in a public namespace/variable like SomeObject and theirfunc is.
If course, what you can do though, like you started to do in your second block, is to manually reproduce the contents of that function and redeclare it. though that wouldn't be able to automatically grab the contents of the function due to its scope.
Upvotes: 0