Is it possible to create or manipulate classes?

63 views
Skip to first unread message

Tyler Gaw

unread,
May 10, 2014, 1:21:38 PM5/10/14
to jstal...@googlegroups.com
In the JSCocoa docs, it says that you can write new and manipulate existing classes.

I'm trying to do the same in JSTalk, but since class is a reserved word it throws an error.

Is there a way to create or manipulate classes in JSTalk?

Example code from the JSCocoa readme https://github.com/parmanoir/jscocoa#syntax
// Write a new Cocoa class in Javascript (inspired by Cappucino)
class MyClass < NSObject
{
    // Custom drawing, calling parent method
    - (void)drawRect:(NSRect)rect
    {
        // do some drawing here
        ...
        // Call parent method
        this.Super(arguments)           
    }
    // Class method
    + (float)addFloatX:(float)x andFloatY:(float)y
    {
        return x + y
    }
}

// Manipulate an existing class
class NSButton
{
    // Add a method to an existing class
    - (void)someNewMethod:(NSString*)name
    {
        ...
    }

    // Swizzle an instance method of an existing class
    Swizzle- (void)drawRect:(NSRect)rect
    {
        // Draw something behind the button
        ...
        // Call original swizzled method
        this.Original(arguments)
        // Draw something in front of the button
        NSBezierPath.bezierPathWithOvalInRect(rect).stroke
    }
}

Gus Mueller

unread,
May 12, 2014, 6:25:23 PM5/12/14
to jstal...@googlegroups.com
On May 10, 2014, at 10:21 AM, Tyler Gaw <m...@tylergaw.com> wrote:

In the JSCocoa docs, it says that you can write new and manipulate existing classes.

I'm trying to do the same in JSTalk, but since class is a reserved word it throws an error.

Is there a way to create or manipulate classes in JSTalk?

There isn’t.  JSTalk being implemented on JSCocoa is an implementation detail- and I had never intended for you to be able to create classes in JS land, at least in the early versions.

CocoaScript (JSTalk’s successor) is using the Mocha bridge for its backend, which doesn’t support creating classes.  Maybe someday, but not today.

-gus

Tyler Gaw

unread,
May 12, 2014, 11:12:42 PM5/12/14
to jstal...@googlegroups.com
OK, good to know. Thanks Gus.

Maybe there's another way to accomplish what I'm trying to do. I'm trying to use a method that requires a delegate like beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: As far as I can tell, the delegate is required for setting a didEndSelector. Is there any type of workaround for that?

Gus Mueller

unread,
May 13, 2014, 12:44:16 PM5/13/14
to jstal...@googlegroups.com
On May 12, 2014, at 8:12 PM, Tyler Gaw <m...@tylergaw.com> wrote:

Maybe there's another way to accomplish what I'm trying to do. I'm trying to use a method that requires a delegate like beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo: As far as I can tell, the delegate is required for setting a didEndSelector. Is there any type of workaround for that?

Not that I can think of.

A class could be created and loaded at runtime to handle this stuff- but it’d have to be written in Obc.

-gus
Reply all
Reply to author
Forward
0 new messages