|
JavascriptObjCClass
JSCocoa lets you write ObjC classes in Javascript. For now, you can only create new classes by deriving them from an existing class — you cannot create toplevel classes. Sample ObjC classdefineClass('MyButton < NSButton',
{
myOutlet : 'IBOutlet'
,myAction : ['IBAction',
function (sender)
{
...
}]
})Overloading a methodAfter creating a class named MyButton derived from NSButton, you can define a javascript method drawRect: that will be called when MyButton needs drawing. JSCocoa does that by creating a FFIclosure tied to drawRect:. When overloading drawRect:, JSCocoa will automatically get the MethodEncoding from the parent function by calling method_getTypeEncoding on NSButton's drawRect:. Creating a new methodWhen using delegation (methods like awakeFromNib or WebKit's policy delegates), JSCocoa can't infer the MethodEncoding as the method is not defined in the parent class — you need to specify it yourself. Defining methods like awakeFromNib is done by specifying their types : ['void', 'void'] for awakeFromNib. ,awakeFromNib : ['void', 'void', function()
{
// awake code here
}]IBActions are specified with 'IBAction'. ,executeAction : ['IBAction', function ()
{
// action code here
}]'IBAction' is shorthand for ['void', 'id']. |
The problem I have always had with Obj-C/Cocoa is memorizing the API because of the notation. It's not like I can figure out what to do but I work so much slower. This might just get me around that.