Posts

Showing posts from October, 2008

3, 2, ... 1, ... Coming Soon!

Guys, I can't wait for Ubuntu 8.10 and what's up in the website? The countdown now shows a "Coming Soon" message ... I mean, a little news about when is that difficult? I know there is a kernel problem since 27 October, and i hope you updated are updating the release to fix that problem with the 8.10 ... but shall you tell us more? Kind Regards And thank You :)

jQuery plug-in distributed elements via If, ElseIf, and Else

After a couple of instant and consecutive releases, I ended up with a plug-in that fits minified into 240 bytes :) Here is the official plug-in page , while this is the example page and this is the summary: ;jQuery.fn.extend({ // Andrea Giammarchi - Mit Style Licence - V0.2 If:function(fn){ var __If__ = this.__If__ || this, $ = __If__.filter(fn); $.__If__ = __If__.filter(function(){return !~$.index(this)}); return $; }, Else:function(){ return this.__If__; }, Do:jQuery.fn.each }); jQuery.fn.ElseIf = jQuery.fn.If;

jQuery If, ElseIf, and Else plugin

Update Guys, I have to admit I created silly prototypes while all I need were much more simpler than I though :) Enjoy last version ! ------------------------------------- Try to imagine a page like this one: <body> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> </body> .. and now try to imagine something like this: function oddNumbers(){ // return true if element contain an odd number return $(this).text() & 1; }; $(function(){ $("div") .If(function(){return $(this).text() == "3" || $(this).text() == "5"}) .text("match the 3 or 5 check") .ElseIf(oddNumbers) .text("odd numbers") .ElseIf(function(){return $(this).text() == 2}) .Do(function(){ // if you need a closure ... $(this).text(...

Subclassing JavaScript Native String

Just a quick post about subclassing native String constructor. All we need is to redefine valueOf and toString methods. function $String(__value__){ this.length = (this.__value__ = __value__ || "").length; }; with($String.prototype = new String) toString = valueOf = function(){return this.__value__}; With incoming V8, TraceMonkey, Squirrelfish, and other (if any ...) advanced engines that transforms repeated code into machine one, performances wont be a problem anymore and everybdy could create its own implementation of the String. Of course, these statements will be preserved: var s = new $String("abc"); s instanceof String; // true s.constructor === String; // true while typeof will return an object but we can easily use another method such: $String.prototype.type = function(){ return "string"; }; alert(typeof s); // object alert(s && s.type()); // string concat as other native methods willl work, but returned object, unfortunatly, won...

Big Douglas begetObject revisited recycling a unique function

With the precedent post I realized I wrote a really tricky way to extend inline a function.Next code is the snippet summary: MyExtendedConstructor.prototype = function(Function){ var callee = arguments.callee; if(!(this instanceof callee)){ callee.prototype = Function.prototype; return new callee; } }(MyBaseConstructor); Above code uses the closure itself to create the intermediate constructor. The reason i chose this way to operate that task was: why should I use another function when I already have one that is the closure itself? Well done, one function instead of two ... but wait a second, why should I create a different function everytime instead of recycle a single one? Douglas Crockford begetObject concept In one of His historical posts , Big Douglas describes simple JavaScript inheritance and object cloning using an intermediate constructor. This is the Object.create function: Object.create = function (o) { function F() {} F.prototype = o; ...

a new Relator object plus unshared private variables

This is a Robert Nyman 's post dedicated reply, about JavaScript inheritance and alternatives and private variables . First of all, I would like to say thanks to Robert for both interesting articles He is writing about JS inheritance, and a link to a personal comment which aim was to bring there my "old" documentation about classical JavaScript inheritance and usage of prototype, closures, and public, privileged, or private, scope when we create a constructor. About Last Rob's post talk about private variables, describing them as shared, if present outside the constructor, and valid only for singleton instances. This is true, and could cause a lot of headache if we are not truly understanding closures and prototype shared methods behaviour, but there is a way to use this peculiarity about shared private variables to create dedicated private variables. Before I will write about it, let's look into a generic shared private variable example: Click = function(){ ...

JavaScript bits and bops :-)

This post is about a couple of probably useful JavaScript functions, that on daily basis could make our code smarter ;) String.prototype.Replace Ok, ok, a prototype into a native constructor is not a good start point, but this one, strings dedicated, is probably one of those "must have" protos, a Replace with multiple inputs, as is for PHP. String.prototype.Replace = function(replace){return function(RegExp, String){ if(!(RegExp instanceof Array)) RegExp = [RegExp]; if(!(String instanceof Array)) String = [String]; for(var result = this, i = 0, l = 0, index = RegExp.length, length = String.length; i result = replace.call(result, RegExp[i], String[l return result; }}(String.prototype.replace); With above prototype it is possible to search and replace with multiple RegExps and multiple replacements. The proto works as native replace one, but it is possible to perform a replace like this as well: "abc".Replace([/a/, /b/, /c/], ...