ranking Sort Sort   |   date Sort Sort   |   member Sort Sort

Date Submitted Mon. Jul. 6th, 2009 3:00 AM
Revision 1
Scripter Cloudgen
Tags JavaScript | jquery | property | Prototype
Comments 0 comments
Add property to Javascript Obj in "jQuery Style", see detail:here
Date Submitted Fri. Jan. 11th, 2008 11:35 PM
Revision 1
Scripter Fordiman
Tags ActionScript | Bind | curry | JavaScript | Prototype
Comments 1 comments
Ok, so here's the deal. I've started to dally with Flash a bit, and I'm feeling all naked without my nifty Prototype functionality.

Most important to me are the bind() and curry() functions (though, others will rear their heads as my AS code gets more complex), Object.extend, and $A. They are para-ported here.
Date Submitted Mon. Dec. 24th, 2007 2:34 AM
Revision 1
Scripter Fordiman
Tags "data hidng" | Class | JavaScript | Prototype
Comments 3 comments
Ok, so I was getting frustrated with the inability to hide data in javascript, as well as a number of other concerns with existing class structures (the requirement to use this.constructor in lieu of self, for example).

As a result, I've thrown together a nice little class constructor that accepts a class definition, as well as a class extender that allows you to build a new class from an existing one, with full access to all that private stuff.
Date Submitted Thu. Dec. 20th, 2007 6:48 PM
Revision 1
Scripter Fordiman
Tags JavaScript | promise | Prototype
Comments 1 comments
This is a version of my previous entrant, Promise, that will work with prototype, but does not require it. It's slightly more advanced, using .apply here and there.

It's basically a function to allow any other function to poll. Polling is generally regarded as bad practice in object oriented code, but can make very simple the matter of, for example, running a bit of code only after a single-run event (like onload) occurs (whether that be in the future or past), another unrelated bit of code needs to be hack-tracked, or any other generic condition.

Note that when the function runs, it doesn't necessarily run within the scope that's called it. Its context is set to itself, rather than its normal context, and it's asynchronous, so you'll not get a return value. If you use Prototype, you can bind the function and it'll behave as it should in terms of context, but I'm unaware of a way to cause an asynch function to block execution - and you'd really rather that not happen anyway, trust me.
Date Submitted Fri. Dec. 14th, 2007 4:41 PM
Revision 1
Scripter Fordiman
Tags "javascript" | Form | Prototype
Comments 0 comments
Prototype's Form.Methods library has a nice piece of code called 'serialize', which, if passed with the optional 'hash' parameter set to true, will convert an HTML form into a Javascript Object.

I was dismayed, however, to find that there was no way to reverse it; just as simply load a form using a Javascrip object.

So, here's one. enjoy.
Date Submitted Fri. Sep. 28th, 2007 2:08 AM
Revision 1
Scripter Fordiman
Tags JavaScript | promise | Prototype
Comments 6 comments
Here's a quick lil' addon for Prototype that I use often.

Prototype.Promise(condition, action, interval)
condition is a string that you want met before an action is run.
action is a function that does the action.
interval is the polling rate for condition in seconds, and defaults to 1

So, for example, you may want function foo to run, but only once bar has been set:

function foo(a,b) {
var thingy = {

Then, in some point in the mysterious future, thingy.retVal gets set to 5, at which point, the Promise goes into effect, and thingy.retVal becomes 10.

Where I find this particularly useful is in making sure that a document is loaded before doing something (condition="$$('body').length>=1), as you can see it used for the include functions.

Speaking of which, the following include functions are great for getting scripts and stylesheets into your page. I won't bother with examples, as they're pretty straightforward.

Meanwhile, Prototype.scriptPath will point to wherever in your server's heirarchy Prototype was loaded from. The regex, you'll note allows for names like prototype.compressed.js, prototype.modified.js, 2007-09-28.prototype.js, etc - just in case you want to keep track of your various hacks of Prototype, as I do.
Date Submitted Thu. Sep. 27th, 2007 8:02 AM
Revision 1
Scripter Fordiman
Tags JavaScript | PHP | Prototype | serialize
Comments 2 comments
This is the final version of my Javascript serializer targetted at PHP.

The point:
Objects are most easily passed over the network as serialized strings. Between serialization and unserialization, serialization is by far the easier of the two. Since object passing can sometimes be a process-hungry thing, we want to do things as quickly as possible.

My solution is to always do the hard part in compiled code, while doing the easy part in script. That is, whichever way you're passing an Object, you want to pass it in a natively decoded format for the target.

Since I work mostly in PHP, this meant writing a module that would be able to generate a string that can be decoded with PHP's unserialize() function into a PHP Associative Array (or other applicable type).

This lib REQUIRES the Prototype lib. You can hack prototype out of it, of course (by replacing the references to Object.extend() with explicit assignments), but I can't imagine why you'd want to bother; it's used mostly with Ajax.Request anyway.

Previous versions of this code would add the .toPHP() member to the Object prototype. After trying to enumerate things, I found that this is a REALLY bad thing to do, as toPHP springs up where it's not wanted in ALL objects. As a result, I've opted to go the Prototype route and apply it as a member of the Object object.

Please note that if you pass a serialized string to PHP via GET or POST, you'll need to stripslashes() before unserialization.

Javascript sample of use:

var myObject = {
timestamp: new Date(),
float: 3.1415926539,
number: 42,
func: function () {
alert('Member functions are always omitted from serialization');


Sample of subsequent unserialization in PHP (passed via POST as 'myobject')


array(7) {
string(5) "value"
array(3) {
string(5) "Array"
string(2) "of"
string(7) "strings"