From Wikibooks, open books for an open world
Jump to navigation Jump to search

Objective-J has two types of objects:

  • Native JavaScript objects and Objective-J objects.
    • Native JS objects are exactly what they sound like, the objects native to JavaScript.
  • Objective-J objects are a special type of native object added by Objective-J.

These new objects are based on classes and classical inheritance, like C++ or Java, instead of the prototypal model.

Classes[edit | edit source]

Creating a class in Objective-J is simple. Here’s an example of a Person class that contains one member variable, name:

@implementation Person : CPObject
       CPString name;

The beginning of a class is always the keyword @implementation, followed by the class name. The third term, after the colon, is the class you want to subclass. In this case, we’re subclassing CPObject, which is the root class for most classes. You don’t need a superclass, but nearly all the time you will want one.

After the declaration, a block enclosed with brackets is used to define all your member variables. Each variable is declared on its own line with a type and variable name, and a semicolon. Technically the type is optional, but its highly recommended. Declaring your member variables is important, because any variable used elsewhere in your class that isn’t declared will automatically become a global variable.

To end a class declaration, add the keyword @end.