background preloader

Oop

Facebook Twitter

JavaScript Method Overloading. In a side project that I’ve been working on I built a quick-and-dirty function for doing simple method overloading. For those of you who aren’t familiar with, it’s just a way of mapping a single function call to multiple functions based upon the arguments they accept. Here’s the function in question: // addMethod - By John Resig (MIT Licensed)function addMethod(object, name, fn){ var old = object[ name ]; object[ name ] = function(){ if ( fn.length == arguments.length ) return fn.apply( this, arguments ); else if ( typeof old == 'function' ) return old.apply( this, arguments ); };} and here is how you might use it: function Users(){ addMethod(this, "find", function(){ // Find all users... }); addMethod(this, "find", function(name){ // Find a user by name }); addMethod(this, "find", function(first, last){ // Find a user by first and last name });} Or, if you wanted to use it with an object prototype: function Users(){}addMethod(Users.prototype, "find", function(){ // Find all users...})

Ruby's Open Classes and Inheritance in JavaScript. Using the compact extend function to simulate class-based inheritance in JavaScript we have many of the same features of Ruby classes. One important feature is that classes remain open and that changes in the superclass are automatically also in the subclass. Through the extend function's natural chaining of prototypes we can have this open and inheritable changes.

Below are the Ruby and JavaScript versions of the same example to show this at work. Ruby class Person def initialize(first, last) @first = first @last = last end def to_s @first + ' ' + @last end end class Employee < Person def initialize(first, last, id) super(first, last) @id = id end def to_s super + ': ' + @id.to_s end end peter = Employee.new('Peter', 'Michaux', 3) puts peter # Peter Michaux: 3 # open the Person class and add a new method class Person def reverse_name @last + ', ' + @first end end puts peter.reverse_name # Michaux, Peter. Darron schall :: Multiple Inheritance in ActionScript 3. Did you know ActionScript 3 supports multiple inheritance?

Here's how... Multiple inheritance is actually possible with AS3, despite popular belief that it's not supported. It's not really multiple inheritance in the true sense of the word going by the strict definition... but there's a way to get the job done in AS3 that behaves almost like the real thing. What I'm about to show you is not something I would advocate as best practice. Nor is it something that should be (ab)used simply because its available. Various disclaimers aside, here we go... When you to simulate use multiple inheritance, there are three things you need to do: You need to create an interface that defines the methods you want to use. You need to create a default implementation for that interface. You need to merge the default implementation for the interface into the class you want the methods available in, and mark the class as implementing the interface. ...drum roll...

This is where it gets ugly, but stay with me. OOP ActionSript 2.0. Project Blog » Class-based OOP in Javascript done right. Max, Adam and I have been working on a scheme to support OpenLaszlo's LZX language in pure Javascript. As explained in our blog entries, LZX is a blend of class-based and prototype-based object-oriented programming, with an emphasis on class-based programming (because that seems the more widely accepted paradigm). Our current project is to build a new back-end for the LZX compiler to emit "browser Javascript" (i.e., Javascript that will run in all browsers) and to adjust the OpenLaszlo Runtime to also run in browser Javascript. The goal is to make OpenLaszlo run directly in the browser as any good AJAX platform should. My post over the weekend was our latest version, but right after I posted it, I realized there was still a lot of room for improvement. The basic idea was there, but as Max observed, it's really not nice to modify the built-in Javascript classes.

One might even claim that Object.prototype is verboten. Adding class-based inheritance to Javascript is a popular pastime.