Interface JSExport


  • public interface JSExport
    [@protocol] JSExport provides a declarative way to export Objective-C objects and classes -- including properties, instance methods, class methods, and initializers -- to JavaScript. When an Objective-C object is exported to JavaScript, a JavaScript wrapper object is created. In JavaScript, inheritance works via a chain of prototype objects. For each Objective-C class in each JSContext, an object appropriate for use as a prototype will be provided. For the class NSObject the prototype will be the Object prototype. For all other Objective-C classes a prototype will be created. The prototype for a given Objective-C class will have its internal [Prototype] property set to point to the prototype created for the Objective-C class's superclass. As such the prototype chain for a JavaScript wrapper object will reflect the wrapped Objective-C type's inheritance hierarchy. JavaScriptCore also produces a constructor for each Objective-C class. The constructor has a property named 'prototype' that references the prototype, and the prototype has a property named 'constructor' that references the constructor. By default JavaScriptCore does not export any methods or properties from an Objective-C class to JavaScript; however methods and properties may be exported explicitly using JSExport. For each protocol that a class conforms to, if the protocol incorporates the protocol JSExport, JavaScriptCore exports the methods and properties in that protocol to JavaScript For each exported instance method JavaScriptCore will assign a corresponding JavaScript function to the prototype. For each exported Objective-C property JavaScriptCore will assign a corresponding JavaScript accessor to the prototype. For each exported class method JavaScriptCore will assign a corresponding JavaScript function to the constructor. For example: