Archive for the ‘Javascript’ Category
changing function’s context in javascript
Today I learned how to change the context of a function in javascript. When we do:
function test() { this.foo = "Hello from " + this + " context."; } test(); alert(foo); // will show "Hello from [object Window] context.";
foo will belong to the global context. In other words global object foo will be created. We can change the context to some other object by doing:
var otherContext = {}; test.call(otherContext); alert(otherContext.foo); // will show "Hello from [object Object] context."
In this case foo will belong to otherContext.
JQuery 1.3
New release contains new selector engine called Sizzle (the fastest css selector out there) and live events (which work similar to the live query plugin).
More details can be found here
Peppy fast css3 selector engine
Peppy is a small and very fast css3 selector written by James Donaghue. Here is how you can use it:
var selector = "div"; var context = "#elementId"; var q = peppy.query(selector, context);
JavaScript closures
Nice presentation about JavaScript closures posted by Stuart Langridge
Live Query plugin for jquery
It’s annoying to rebind events to their targets after Ajax request. You can read more about it here.
In order to fix it you can use Live Query plugin.
Instead of doing:
$('a').click(dosomething);
$('#htmlElement').load('server.php',function() {
$('a').click(dosomething);
});
you can now do:
$('a').livequery('click',dosomething);
$('#htmlElement').load('server.php');
javascript essentials
Dan Webb posted his list of useful javascript pieces. I have to admit that $(), event handler and getting elements by class name makes your life easier. I would add JQuery to this list, especially when you have some bigger project in mind.