Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Added Class.prototype.define, which is a reference to the previous …

…implement method
  • Loading branch information...
commit 95559d613de66fcaad0d3d48bc6cf6026d2b8002 1 parent ffe4068
@keeto authored
Showing with 13 additions and 9 deletions.
  1. +4 −4 README.md
  2. +9 −5 Source/Class.PatternMutators.js
View
8 README.md
@@ -77,10 +77,10 @@ This mutator has a pattern that states it should be applied to any key in a clas
Instead of creating a separate `Protected` key in our class declaration, we simply added a `protected ` qualifier at the start of the method name. The effects, though, are the same as our previous declaration.
-Class Method: defineMutator
----------------------------
+Class Static Method: defineMutator
+----------------------------------
-This extension provides a single function, `Class.defineMutator`, that can be used to define both key-based and pattern-based mutators
+This extension provides a function, `Class.defineMutator`, that can be used to define both key-based and pattern-based mutators
### Syntax:
@@ -106,7 +106,7 @@ This extension provides a single function, `Class.defineMutator`, that can be us
Implementation Notes
--------------------
-- This extension wraps the default `Class.prototype.implement` method in order to add the pattern-based mutator check. Internally, though, it still uses the default `implement` method in order to add the members to the class' `prototype`.
+- This extension wraps the default `Class.prototype.implement` method in order to add the pattern-based mutator check. Internally, though, it still uses the default `implement` method in order to add the members to the class' `prototype`. The default `implement` method is reimplemented as `define`.
- Pattern mutators are tested in reverse: the last mutator declared will be the first one tested. This is due to the use of a reverse `while` loop to speed-up the matching process.
- Pattern mutators take precedence over key-based mutators, and a pattern mutator could be applied *together* with a key-based mutator in some cases.
- Several mutators can be used in a single key as long as the mutators use flexible regexp patterns and always invoke `implement` method.
View
14 Source/Class.PatternMutators.js
@@ -11,7 +11,7 @@ copyright: Mark Obcena
requires: Class
-provides: Class.defineMutator
+provides: [Class.defineMutator, Class.define]
...
*/
@@ -44,7 +44,8 @@ Class.defineMutator = function(key, fn){
return this;
};
-var oldImplement = Class.prototype.implement;
+var define = Class.prototype.implement;
+Class.implement('define', define);
var implement = Class.prototype.implement = function(key, value, retain){
var mutator = lookup(key);
@@ -53,12 +54,12 @@ var implement = Class.prototype.implement = function(key, value, retain){
mutator[0].unshift(value);
value = mutator.shift();
}
- return oldImplement.call(this, key, value, retain);
+ return define.call(this, key, value, retain);
}.overloadSetter();
// Default Mutators
Class.defineMutator(/^protected\s(\w+)/, function(fn, name){
- this.implement(name, fn.protect());
+ this.define(name, fn.protect());
});
Class.defineMutator(/^linked\s(\w+)/, function(value, name){
@@ -72,7 +73,10 @@ Class.defineMutator(/^static\s(\w+)/, function(fn, name){
// Reimplement "implement" in all classes..
for (var i in window){
var klass = window[i];
- if (klass instanceof Function && typeOf(klass) == 'class') klass.implement = implement;
+ if (klass instanceof Function && typeOf(klass) == 'class'){
+ klass.implement = implement;
+ klass.define = define;
+ }
}
})();
Please sign in to comment.
Something went wrong with that request. Please try again.