Recent Weblogs

Links I like

Javascript Class

Function is the base object that so much of the language relies on. It allows scope, inheritance, composition and many other patterns of programming that allows developers to create the systems that they do. Prototype.js has enhanced this object to allow for ease of use when creating these systems. In particular the assets that assist this process is, Class.create, Function.prototype.bind and Object.extend

Class.create

Class.create returns a function that executes the initialize function that all classes must inherit at some point before becoming instantiable. What this does is everytime you create a new instantation of your class it returns a function prebound to that instance and executes the initialize function for you. This is very similar to having a constructor function that executes everytime you create a new instance of the class.

Object.extend

Object.extend is a function that allows easy implementation of composition. It accepts two variables, the destination being the first, which is the variable that you want to inherit properties, and the source which is the variable that you want to propogate its properties into the destination.

Function.prototype.bind

Function.prototype.bind extends the native functionality of all functions created. Bind is a property of all functions that returns a function that is prebound(using the apply property) to the variable sent to the first parameter of bind. This functionality reveals its potential in many circumstances, I have found it useful when attaching event listeners and when iterating through collections.

View Code

So lets take a look at a little example of an implementation of a javascript class

var Super = Class.create();
var Sub = Class.create();

Object.extend(Super.prototype,
                    {
                        name : "Super",
                        initialize : function(param){
                        
                        
                        },
                        inheritance : function(){
                        
                            alert("I have inherited this function  -- All ye mighty, look upon my works and despair."  + this.name);
                            
                        }
                    
                    }
                );
Object.extend(Object.extend(Sub.prototype, Super.prototype),
                {
                    name : "Sub",
                    initialize : function(obj){
                        
                        this.obj = $(obj);                            
                        Event.observe(this.obj, "click", this.inheritance.bindAsEventListener(this));                                    
                    
                    }
                    
                    
                }
            );                    
            
					
For an example of this at work click the button -->

Comments

September 18, 2008Güncel driver download sitesi - actual driver download site

Thanks

September 18, 2008Guncel driver download sitesi - actual driver download site

Thanks

September 27, 2008Games

Thanks

October 29, 2008figurin

Thank you very much for this information.
Good post thanks for sharing.
I like this site ;)

December 22, 2008Müzik Dinle

Good post thanks for sharing.

December 22, 2008Gazeteler

I like this site ;)

January 13, 2009Mobil X Mobiler

In computer science, a subroutine or subprogram (also called procedure, function, method, or routine) is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code. (The name "method" is mostly used in object-oriented programming, specifically for subroutines that are part of objects or object classes.)

January 20, 2009Web Design Birmingham

Although best known for its use in websites (as client-side JavaScript), JavaScript is also used to enable scripting access to objects embedded in other applications.

Name
Site
Comment
  CAPTCHA Image
Reload Captcha Image
Captcha