hijacking - question

0 views
Skip to first unread message

RA

unread,
Nov 20, 2009, 9:07:14 PM11/20/09
to TiddlyWiki
In BreadcrumbsPlugin, I see

Story.prototype.displayTiddler = function(srcElement,tiddler)

Shouldn't it have the same number of parameters as the one being
hijacked, i.e.

Story.prototype.displayTiddler=function
(srcElement,tiddler,template,animate,unused,customFields,toggle)


Thanks.
--R

Tobias Beer

unread,
Nov 20, 2009, 10:54:07 PM11/20/09
to TiddlyWiki
See, that's one of the funny - if not brilliant things - with
Javascript:

a=function(a,b){alert(arguments.length);}
a('foo','bar','baz');

Guess, what arguments.length is? 3! Great, nothing's lost! How
about...

out=function(a,b){return arguments[0]+arguments[1];}('foo','bar');
alert(out);

You guessed right... out alerts as 'foobar' ...declared, anonymous,
invoked and return value assigned... all in one step.


Or, if you want to go really nasty...

x='foo';
y=function(obj){out=[];for(var p in obj)if(typeof(obj[p])=='object')
out.push(arguments.callee(obj[p]));out.push(x);return out;}
(someObject);

Declared, recursive, anoymous, invoked while using closure and return
value assigned... all in one statement!
All honours to Eric Shulman, for explaining this stuff. ;-)


Tobias.

Tobias Beer

unread,
Nov 21, 2009, 6:28:06 AM11/21/09
to TiddlyWiki
Hi again RA,

So, here's maybe the missing link between your question and my
previous reply...


1) For a "proper" hijack you always start with storing the original
function...

if (Story.prototype.someCoreFunction_myBackupOf==undefined)
Story.prototype.someCoreFunction_myBackupOf
=Story.prototype.someCoreFunction;


2) Overwriting the original, your function is then declared based on
the previous functions premises, possibly customized to what you
actually need...

Story.prototype.someCoreFunction = function(my,args) {
//...


3) However, somewhere in the code of your replacemet - preferably at
the very end - you are to call the original function, otherwise you're
not quite hijacking, but rather merely overwriting the original...

this.someCoreFunction_myBackupOf.apply(this,arguments);


Note the __apply(this,arguments)__ part, shoving over all of what you
possibly ignored. There you go, another how-to-properly hijack in 5
Minutes plus some more...

As for me, this - and what I mentioned before, and also object
literals, array shorthands, functions as firstclass objects, etc... -
is the kind of stuff where javascript slowly but profoundly starts
making you feel that it's not just some cheap-ass browser scripting
engine, but rather in the realm of the more powerful tools for
expressing yourself in code around today ...lean and mean ;-)

Tobias.
Reply all
Reply to author
Forward
0 new messages