Friday, March 5th, 2010

Friday fun: Let’s translate YUI3 to jQuery

Category: jQuery, Library, YUI

<>p>I just came across this wonderful Gist on gitHub:

javascript
< view plain text >
  1. var $;
  2. YUI().use('*', function(Y){
  3.   $ = Y.get;
  4.   for(var p in Y) {
  5.       $[p] = Y[p];
  6.   }
  7. });
  8.  
  9. // test
  10. $('body').append("boo!");

In case you want to use YUI3 but really really like jQuery syntax :) OK, it breaks the whole sandboxing idea of YUI3, but that’s a small price to pay, right?

Related Content:

Posted by Chris Heilmann at 8:52 am
11 Comments

++---
2.4 rating from 71 votes

11 Comments »

Comments feed TrackBack URI

Do not tease the jQuery gods, they will rain down $ from the global window and smite thee!
(I dunno either, it’s Friday. :D)

Comment by Schill — March 5, 2010

Does it *need* to break the sandboxing though? Why not just do this:

YUI().use(‘*’, function(Y){
var $ = Y.get;
for(var p in Y) {
$[p] = Y[p];
}
// test
$(‘body’).append(“boo!”);
});

Comment by Fotiman — March 5, 2010

@Schill Do you really think it’s because of Friday that we see this kind of s…tuff?

Comment by fpiat — March 5, 2010

Y.get is deprecated (in favor of Y.one), and it would be more jQuery-like to use var $ = Y.all; which manages lists of nodes. But obviously in jq, the $ is overloaded to do a lot more than fetch nodes.

Comment by lsn — March 5, 2010

What can I say…jQuery, the framework that managed to make javascript look like Perl…

Comment by RPBouman — March 5, 2010

RPBouman,

What? jQuery looks like a DSL. Perl looks like… pretty much whatever you want it to look like, from seemingly random characters to fairly sane scripting.

Comment by eyelidlessness — March 6, 2010

Hey, you folks all know that there are JavaScript obfuscation tools that will make your code impossibly difficult to read and ultimately maintain down the road automatically, right? You don’t actually have to write the code like that yourself!

Oh, sorry, did I forget to wrap that in sarcasm tags?

Comment by fzammetti — March 6, 2010

RPBouman: some, like me, would sy that is a good thing. But unfortunately that isn’t the case, but see pQuery (on the CPAN).

Comment by monkeyvegas — March 6, 2010

Yes, and no. Y.get and .one return a YUI Node not a DOM Element .. Node != Element

Comment by jminkler31 — March 8, 2010

Well it would actually nice if you also posted a reference to the gist.. to the author of it some credit..

Comment by V1 — March 9, 2010

@V1 the author is a good friend of mine and less annoyed about this than you are.

Comment by Chris Heilmann — March 12, 2010

Leave a comment

You must be logged in to post a comment.