Web Reflection: jQuery If, ElseIf, and Else plugin (2)
share
digg
by
noreply@blogger.com (Andrea Giammarchi) (0)
on
Web Reflection (0)
1 month, 1 week
ago
permalink
UpdateGuys, 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}) ...
Big Douglas begetObject revisited recycling a unique function (1)
share
digg
by
Andrea Giammarchi (0)
on
Web Reflection (0)
1 month, 1 week
ago
permalink
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, ...
Subclassing JavaScript Native String (1)
share
digg
by
noreply@blogger.com (Andrea Giammarchi) (0)
on
Web Reflection (0)
1 month, 1 week
ago
permalink
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 ...
a new Relator object plus unshared private variables (1)
share
digg
by
Andrea Giammarchi (0)
on
Web Reflection (0)
1 month, 2 weeks
ago
permalink
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.AboutLast Rob's post talk ...