|
|
Introduction
Joose allows easy probing, introspection and extention of classes and their objects.
All Joose objects have a property called meta that can be used to access the object's meta object. Classes and their instances share the same meta object and thus have the same meta interface. All method's decribed on this page may be called on the meta object of classes and instances. Unless you use Prototypes they will always affect the class as well as all instances.
Probing
Method: can(methodName)
Returns true if there is an instance method called methodName
// Query whether a instances of MyClass could execute a method called methodName MyClass.meta.can(methodName) // Query whether an instance could execute a method called methodName myObject.meta.can(methodName)
Method: classCan(methodName)
Returns true if there is a class method called methodName
// Query whether MyClass could execute a method called methodName MyClass.meta.classCan(methodName)
Method: isa(classObject)
Returns true if the object or class is of the same class or a sub class of classObject.
// Query whether MyClass is a subclass of SuperClass MyClass.meta.isa(SuperClass) // Query whether the class of myObject is a subclass of SuperClass myObject.meta.isa(SuperClass)
Method: does(role)
Returns true if the object or class implements the role.
// Query whether MyClass implements the role MyRole MyClass.meta.does(MyRole) // Query whether myObject implements the role MyRole myObject.meta.does(MyRole)
Instance Creation
Method: instantiate(optionalParameters)
This is an alternative way to create new instances of a class. The optionalParameters will be passed to the initializer of the class.
var object1 = MyClass.meta.instantiate() var object2 = object1.meta.instantiate()
Introspection
Method: getMethodObject(methodName)
This method will return the method of name methodName if it exists. The returned method object will be of the type Joose.Method.
Method: getAttribute(attrName)
This method will return the attribute of name attrName if it exists. The returned attribute object will be of the type Joose.Attribute.
Method: getAttributes()
This method returns a map of attribute names and attributes objects.
Method: getClassObject()
Returns the class object that is represented by the meta object.
Method: getInstanceMethods()
Returns an array of instance method objects with all instance methods of the class (including methods from super classes).
Method: getClassMethods()
Returns an array of class method objects with all class methods of the class (including methods from super classes).
Method: getSuperClasses()
Returns an array of all direct super classes of the class.
Method: getRoles()
Returns and array of all roles that the class implements.
Class Extention
Method: addMethod(name, function, properties)
This method will add a method of name name with the function body function to the class and all instances. The properties will be passed to the method metaclass (currently none are supported.
Existing methods will be replaced.
Method: addAttribute(name, properties)
This will add an attribute of name name to the class and all instances. The properties have the same form as those which are passed to the has-keyword during class-building.
Existing attributes will be replaced, but their values will stay intact.
Method: addSuperClass(classObject)
Make classObject a super class of the class and all instances. This will not override any existing methods or attributes.
Instance Extention
detach
You may at any time detach an instance from its class by calling detach() directly on the instance. The instance now has its own meta object. Calling methods on this meta object will not have any effect one the former class of the instance.
instance.detach
instane role applicaton
You may also apply a role to an instance. This will automatically detach the instance from its class.
Role.meta.apply(anInstance)
Sign in to add a comment
