From Bauman National Library
This page was last modified on 1 June 2016, at 17:01.
Paradigm Multi-paradigm: reflective, object oriented, imperative, scripting functional
Designed by Cappuccino Core Developers and community
First appeared 2008
Typing discipline dynamic, weak
License LGPL
Website http://www.cappuccino-project.org/
Influenced by
Objective-C, JavaScript

Objective-J is a programming language based on Objective-C. It is a superset of JavaScript, which means that any valid JavaScript code is also valid Objective-J code. Objective-J adds traditional inheritance in JavaScript and dynamic schedulingPrograms written in Objective-J need to be preprocessed before being run by a web browser's JavaScript virtual machine. This step can occur in the web browser at runtime or by a compiler which translates Objective-J programs into pure JavaScript code. The Objective-J compiler is written in JavaScript; consequently, deploying Objective-J programs does not require a web browser plug-in. The first widely known use of Objective-J was in the Cappuccino-based web application 280 Slides in 2008. Even though Objective-J can be used (and has been designed) independently from the Cappuccino framework, Objective-J has primarily been invented to support web development in Cappuccino.


Objective-J has two types of objects: native JavaScript objects and Objective-J objects. 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. Creating a class in Objective-J is simple. Here’s an example of a Address class that contains one member variable, name and city:

 @implementation Address : CPObject
     CPString m_name;
     CPString m_city;

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. 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.


In addition to native functions, Objective-J adds methods which are part of the new class system. Let’s add a set of accessor methods to our Address class:

 - (void)setCity:(CPString)aCity
     m_city = aCity;
 - (CPString)name
     return m_name;

These lines go anywhere after the initial @implementation line and instance variable declaration block, but before the @end keyword. Each method signature starts with either a dash (-) or a plus (+). Dashes are used for instance methods, which are methods you can call on instance variables. Following the dash/plus is a return type, in parentheses, after that we declare the method name. Colon shows us that method takes some parameters. When methods have more than one parameter, each parameter is separated by a colon. In the declaration of such a method, parameters are split by a label, followed by a colon, the type, and the parameter name:

 - (void)initWithName:(CPString)aName city:(CPString)aCity
     m_name = aName;
     m_city = aCity;

Using object and classes

Example of object creation:

 var myAddress = [[Address alloc] init];
 [myAddress setCity:"Moscow"];

Method calls in Objective-J are called “messages”, and you send an object a message using bracket notation like this: [object message]. Every class in Objective-J has a special class method called alloc, which returns a new instance of that class. When creating an object we have to call method alloc, then init method. Every class inherits the init method from CPObject. The alloc class method is analogous to the “new” keyword in many languages like C++, Java and C# in that they create a new instance, method init is analogious to constructor.

Reference and dereference

In C/C++ languages you have to dereference references you use. In Objective-J you have to use methods @ref and @deref. Example on C ande Objective-J languages: C:

 int a;
 int *aPtr = &a;
 *aPtr = 5;
 printf("%d\n", *aPtr);  // 5.
 printf("%d\n", a);      // 5.


 var a,
     aRef = @ref(a);
 @deref(aRef) = 5;
 console.log(@deref(aRef));  // 5.
 console.log(a);             // 5.

Once you have a reference you can pass it around, save it, and dereference it as needed. It’s not an actual pointer though so pointer arithmetic is not possible.

Importing Code

One commonly desired technique missing from JavaScript is the ability to import code in the same way that languages like Java or C allow. To that effect, Objective-J adds the @import statement:

 @import <Foundation/CPObject.j>
 @import <AppKit/CPView.j>
 @import "MyClass.j"

There are two types of @import statements. The angle brackets indicate framework code, while the quotation marks indicate local project code. Framework imports use the built in search path mechanism to search for the desired file in any of the defined locations. Local imports only look in the location relative to the importing file.


Categories allow to add methods to a class without needing to create a new subclass or modify the class’s source code. The new method (or methods) become part of all instances of the class once the category is loaded. This is useful in many different scenarios, for example adding methods to built-in classes. If it is wanted all your CPString objects to have a method that would return the reverse string, you could define a category like this:

 @import <Foundation/CPString.j>
 @implementation CPString (Reversing)
 - (CPString)reverse
    var reversedString = "",
        index = [self length];
        reversedString += [self characterAtIndex:index];
    return reversedString;

Now any string can be reversed.

 var myString = "hello world",
     reversed = [myString reverse];
 alert(reversed);  // alerts "dlrow olleh"

The syntax for the category is @implementation, followed by the class you’re adding to, followed by the name of your category in parentheses. Any methods added before the @end keyword will be part of the category. Note that you can’t add instance variables via categories.


Cappuccino is compatible with many of the latest browsers, including: Internet Explorer 7+, Firefox 2+, Safari 3+, Opera 9+, and Google Chrome.


  1. Learning Objective-J
  2. Accilivity NYC Developers Blog
  3. Objective-J