create :: Class |
Class.create() returns a function that, when called, will fire its own initialize method. This allows for more Ruby-like OOP. It also lets you more easily subclass by overriding a parent's constructor.
Example:
var Animal = Class.create(); Animal.prototype = { initialize: function(name, sound) { this.name = name; this.sound = sound; },
speak: function() { alert(name + " says: " + sound + "!"); } };
var snake = new Animal("Ringneck", "hissssssssss"); snake.speak(); // -> alerts "Ringneck says: hissssssssss!"
var Dog = Class.create();
Dog.prototype = Object.extend(new Animal(), { initialize: function(name) { this.name = name; this.sound = "woof"; } });
var fido = new Dog("Fido"); fido.speak(); // -> alerts "Fido says: woof!" |
Prototype API 1.5.0 - prototypejs.org