Archive

Archive for the ‘Presentations’ Category

Duck Punching

March 6, 2012 1 comment

duck punch

… if it walks like a duck and talks like a duck, it’s a duck, right? So if this duck is not giving you the noise that you want, you’ve got to just punch that duck until it returns what you expect.

 

Last night at BrisJS I gave an impromptu lightning talk on the concept of Duck Punching.

And no, I am not going around beating up poor little duckies…

 

Run Ducks Run

 

I’ll show here the brief example I spoke about last night, as I did a very quick overview without explaining it much, but for some better examples and some more info check out this post from Paul Irish.

 

Duck punching also know as Monkey Patching. is an approach to extend or modify the runtime code of dynamic languages (such as JavaScript) without altering the original source code.

So for example I am sure everyone is aware of the jQuery function “toDuck()” and the current bug where the duck says the wrong thing…

jQuery.fn.toDuck= function(){
        this.text("The duck says: Moo!")
    };

On our site we want to use this function to show some ducks.

<div>Hello World</div>
<button>Make it a duck!</button>​
$('button').on('click', function() {
    
    $('div').toDuck();
    
});

Result:

 The duck says: Moo! 

 

OK so we want to change the functionality / fix this bug but we don’t want to go edit the original source as its minified, will make it harder to take upgrades, etc. To achieve this then we can use duck punching to “enhance” this functionality.

 

(function($) {

    // store original reference to the method
    var orig = $.fn.toDuck;

    $.fn.toDuck = function() {
   
        // Do original function
        orig.apply(this,arguments);
            
        if (this.text() === 'The duck says: Moo!') {
            this.append("  *Punch*  Woof!  *Punch* *Punch*  Quack?... *Gives Ducky bread*");
        }
        
    };
}(jQuery));

 

And our new result:

 The duck says: Moo! *Punch* Woof! *Punch* *Punch* Quack?... *Gives Ducky bread* 

 

As you can see the original functionality remains untouched but we get our desired quack.

Should the original method be fixed down the track our condition will fail and the additional functionally will not be run.

This very strange and contrived example can be on jsFiddle here.

 

Categories: JavaScript, Presentations Tags: ,

Promote JS

February 7, 2012 Leave a comment

 

Documentation of the JavaScript API sucks… Well it did.

 

I would assume everyone has as some stage come across the scourge that is w3schools. <- see not linked. Also please don't Google them…

These guys are bad, crap shocking and in many cases just plain old wrong. Unfortunately they take out the top few results in a Google search for most JavaScript methods.

 

Replace ResultsIndexOf Results

 

This has been a problem for some time and even blocking them from your search results is not easy.

Fortunately someone has done something about this.

Enter the MDN JavaScript Reference. This has been around for a while and I know a number of informed people go here directly and use it.

Unfortunately it is not high in the search rankings however thus non informed / beginners are not seeing the gold mine of information.

This is something I thought I don't have much control over and just needed to do my best to tell people not to use w3schools.

 

Last night I attended and spoke at the first BrisJS Meetup for 2012

One of the other speakers Tim Oxely pointed out something which I was not aware of but am very excited about. promotejs.com

 

JavaScript String .sliceJS String .substringJavaScript String .lengthJS String searchJS Array map

 

This is great! I get a simple, easy way to promote a far superior reference site and also assist in the SEO for MDN to hopefully one day knock w3schools off the top.

It might not be time to spam F5 at arewefirstyet.com yet but the day will come!

 

He Likes It!

 

So I implore you. Every chance you get. Add a promotejs.com link on your site, page, blog or anywhere you can get away with it…

 

Unreadable JavaScript performance tricks you will never use.

February 7, 2012 Leave a comment

 

Last night I spoke at the first BrisJS Meetup for 2012

The title of the presentation was “Confusing & completely unreadable JavaScript performance tricks that you will most probably never use.”

 

For those interested the slides for the presentation can be found here.

(Please feel free to send me a pull request to fix my atrocious spelling…)

As this is an impress.js presentation a decent browser is required to view the correctly but the fall back should work if you are still forced to use a trojan…

 

Also as was pointed out by Tim Oxely every chance you get link to these guys… promotejs.com

JS String .substring