My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
JavascriptObjCClass  
Updated Feb 4, 2010 by parman...@gmail.com

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 class

defineClass('MyButton < NSButton', 
{
	 myOutlet : 'IBOutlet'
	,myAction : ['IBAction', 
		function (sender)
		{
			...
		}]
})

Overloading a method

After 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 method

When 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'].

Comment by mrjjwright@gmail.com, Oct 12, 2008

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.


Sign in to add a comment
Powered by Google Project Hosting