Created
July 17, 2009 22:36
-
-
Save swannodette/149319 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var verify1 = function(fn) { | |
return function() { | |
var args = $A(arguments); | |
if($type(args[0]) != "string") | |
{ | |
throw new Error("First arg is not a string"); | |
} | |
else | |
{ | |
console.log("verify1 decorator run"); | |
return fn.apply(this, args); | |
} | |
} | |
}; | |
var verify2 = function(fn) { | |
return function() { | |
var args = $A(arguments); | |
if($type(args[1]) != "string") | |
{ | |
throw new Error("Second arg is not a string"); | |
} | |
else | |
{ | |
console.log("verify2 decorator run") | |
return fn.apply(this, args); | |
} | |
} | |
}; | |
var generator = function(type) { | |
return function(fn) { | |
return function() { | |
var args = $A(arguments); | |
if($type(args[0]) != type) | |
{ | |
throw new Error("First arg is not a " + type); | |
} | |
else | |
{ | |
console.log("generator type checker run") | |
return fn.apply(this, args); | |
} | |
} | |
}; | |
} | |
var MyClass = new Class({ | |
name: "MyClass", | |
aMethod: function(argA, argB) { | |
console.log("Hello from base class aMethod! argA " + argA + ", argB " + argB); | |
console.log(this.name); | |
}.decorate(verify2) | |
}); | |
var MySubClass = new Class({ | |
Extends: MyClass, | |
name: "MySubClass", | |
aMethod: function(argA, argB) { | |
console.log("before parent"); | |
this.parent(argA, argB); | |
console.log("Hello from subclass aMethod! argA " + argA + ", argB " + argB); | |
console.log(this.name); | |
}.decorate(generator("object")) | |
}); | |
var instance = new MySubClass(); | |
instance.aMethod({}, "bar"); // works |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment