I have this somewhat unformed idea about how random javascript code I see in the wild could change for the better. I'm not sure if there's any real performance improvement to be had, but this idea appeals to me really strongly from an elegance and efficiency perspective. And I admit I think it's cool.
Here's some code from a random site online:
Now what if we changed that:
This exact syntax may not actually work because window.event might be evaluated at first execution once rather than each execution of the final redefined function.
But the idea here is that the object detect of e (to see if the browser has passed the eventing object in the first parameter) only occurs once. On subsequent calls to the function, no detect is done anymore, because the function has redefined itself. Now that is efficient!
But once I try to do this for more than one detect, I run into problems. Now I need a version split for each detect. With 3 object detects, I get 8 potential versions, and that's not the kind of coding I'm looking for.
Now, I know that a function can have things added to it. It's just an object, after all.
By the way, deathsequence gets executed 3 times, not 2. Not sure of syntax or if everything I tried in there actually will work. But it's theoretically sound.
Okay, this is all cool. You can manipulate function objects just like other objects, and you can call them as you instantiate them, just like you can do Object.ObjectReturningMethod.NewMethodOnTheReturnedObject in VB. And if that doesn't work you can force evaluation of the object with parentheses and then do your stuff to it, like in the javascript [tt]alert(("a literal string").toUpperCase());[/tt].
But where do a function's successive procedural statements live? They are separated by semicolons, not commas as in the function literal.
Can I reach in from outside the function and only modify the first statement? That would be perfect.
In the meantime, this is the closest I can think of. I am relatively new to javascript, so please forgive the syntax errors, I know I could eventually work this out and solve the weird closure and object reference issues (maybe some thises are appropriate):
The closure and reference stuff is complicated and might kill everything. But then again, maybe not.
Do you see what I'm trying to do? What do you think? Ideas?
[COLOR=black #e0e0e0]For SQL and technical ideas, visit my blog, Squared Thoughts.
The best part about anything that has cheese is the cheese.[/color]
Here's some code from a random site online:
Code:
function cancelEvent(e) {
e = e ? e : window.event;
if(e.stopPropagation) e.stopPropagation();
if(e.preventDefault) e.preventDefault();
e.cancelBubble = true;
e.cancel = true;
e.returnValue = false;
return false;
}
Code:
function cancelEvent(e) {
if(!e) {
cancelEvent = {body(window.event);};
} else {
cancelEvent = body;
}
return cancelEvent(e);
function body(e) = {
if(e.stopPropagation) e.stopPropagation();
if(e.preventDefault) e.preventDefault();
e.cancelBubble = true;
e.cancel = true;
e.returnValue = false;
return false;
}
}
But the idea here is that the object detect of e (to see if the browser has passed the eventing object in the first parameter) only occurs once. On subsequent calls to the function, no detect is done anymore, because the function has redefined itself. Now that is efficient!
But once I try to do this for more than one detect, I run into problems. Now I need a version split for each detect. With 3 object detects, I get 8 potential versions, and that's not the kind of coding I'm looking for.
Now, I know that a function can have things added to it. It's just an object, after all.
Code:
function cry(){alert('boo hoo');}
var f = {abc: 123, dosomething: function(){alert('hello!');}
f.dosomethingelse = function{alert('bye!');};
f.sound = cry;
die = New Function() {alert('auuurrrrgh');}; //not sure of this syntax
f.deathsequence = function() {cry(); dosomethingelse(); die()}();
f.deathsequence();
f["deathesquence"](); // do it again another way
Okay, this is all cool. You can manipulate function objects just like other objects, and you can call them as you instantiate them, just like you can do Object.ObjectReturningMethod.NewMethodOnTheReturnedObject in VB. And if that doesn't work you can force evaluation of the object with parentheses and then do your stuff to it, like in the javascript [tt]alert(("a literal string").toUpperCase());[/tt].
But where do a function's successive procedural statements live? They are separated by semicolons, not commas as in the function literal.
Code:
function abc() {
alert('first');
alert('second');
}
In the meantime, this is the closest I can think of. I am relatively new to javascript, so please forgive the syntax errors, I know I could eventually work this out and solve the weird closure and object reference issues (maybe some thises are appropriate):
Code:
function cancelEvent(e) {
cancelEventFunc = {
1: function() {e = window.event;},
2: function() {e.stopPropagation();},
3: function() {e.preventDefault();},
}
if(e) cancelEventFunc[1] = null;
if(!e.stopPropagation) cancelEventFunc[2] = null;
if(!e.preventDefault) cancelEventFunc[3] = null;
cancelEvent = function(e){
var statement;
for (statement in cancelEventFunc) {
cancelEventFunc[statement]();
}
e.cancelBubble = true;
e.cancel = true;
e.returnValue = false;
return false;
};
return cancelEvent(e);
}
Do you see what I'm trying to do? What do you think? Ideas?
[COLOR=black #e0e0e0]For SQL and technical ideas, visit my blog, Squared Thoughts.
The best part about anything that has cheese is the cheese.[/color]