Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created February 11, 2010 02:26
Show Gist options
  • Select an option

  • Save subtleGradient/301134 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/301134 to your computer and use it in GitHub Desktop.
"method" method mutator
// Current MooTools 1.2 Class subclass syntax
// all methods are wrapped
// to provide protected support
// and `this.parent`
var Dude = new Class({
Extends: Person,
say: function(message){
return this.parent(message + '… dude!');
}
});
// Don't wrap methods by default
// Use Method Mutators to parse property names
// Use a method constructor function to define the method
// Access the special `m` object using a closure
// No binding necessary.
// Since the real function has access to `m` via closure
// you can pass that bunction around all over the place
// without having to bind
// and without having an extra wrapper function
var Dude = new Class({
Extends: Person,
Methods: true, // not necessary in MooTools 1.3+
'method say': function(m){
m.run = function(message){
m.parent(message + '… dude!');
}
}
});
var Dude = new Class({
Extends: Person,
'method say': function(m){
m.run = function(message){
m.parent(message + '… dude!');
}
}
});
var Dude = Person.subclass(function(def){
def ['method say'] = function(m){
m.run = function(message){
m.parent(message + '… dude!');
}
};
});
var Dude = Person.subclass(function(def){
def.method.say = function(m){
m.run = function(message){
m.parent(message + '… dude!');
}
};
});
// No method constructor necessary
// Passes a special `m` object as the first argument
// A disadvantage of this approach is that you can no longer use the function by itself
// with the custom argument it requires a wrapper to pass the magical `m` object
var Dude = Person.subclass({
'method say': function(m, message){
m.parent(message + '… dude!');
}
});
var Dude = Person.subclass(function(def){
def.method.say = function(m, message){
m.parent(message + '… dude!');
}
});
var Dude = Person.subclass(function(){
this.method.say = function(m, message){
m.parent(message + '… dude!');
}
});
// Use the function name itself to set the magical `m` object.
// Bad side effects
// All the private `m` properties are public
// Browser bugs & quirks
// Syntax error in Safari 2 with object literal version
// Pollutes the dubugging info with a million functions named "m"
var Dude = Person.subclass({
'method say': function m(message){
m.parent(message + '… dude!');
}
});
var m = null; // Needed to keep IE from leaking?
// Alternate 1
// Use a function that returns an object and then immediately set a property on that object.
var Dude = Person.subclass(function(def){
def.method('say').run = function m(message){
m.parent(message + '… dude!');
}
var m = null; // Needed to keep IE from leaking?
});
// Alternate 2
// No need for ".method"
// use the run property to implicitly define something as a special "method"
var Dude = Person.subclass(function(def){
def ('say').run = function m(message){
m.parent(message + '… dude!');
}
var m = null; // Needed to keep IE from leaking?
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment