Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "VJET/Typing Object Literals and Function References"

(Formatting, use more bullet lists. Fix minor syntax error in example.)
Line 1: Line 1:
JavaScript object literals provide a way to define and create new objects.  They are frequently used to define data structures and lightweight class instances.
+
JavaScript object literals provide a way to define and create new objects.  They are also frequently used to define data structures and lightweight class instances.
  
Object Literals and Function Refs are frequently used constructs in JavaScript; they can be assigned to variables and properties, returned by functions, and passed as arguments.
+
Object Literals and Function References are frequently used constructs in JavaScript; they can be assigned to variables and properties, returned by functions, and passed as arguments.
  
In this example Point is comprised of two int properties, Line is comprised of two Point properties and the function SimpleLine takes two Point arguments.   We also define two function refs, createLine and isSingularity.
+
In this example:
 +
* Point is comprised of two ''Number'' properties
 +
* Line is comprised of two ''Point'' properties  
 +
* Define the function ''SimpleLine'' that takes two ''Point'' arguments
 +
* Define two function references: ''createLine'' and ''isSingularity''
  
 
<source lang="javascript">
 
<source lang="javascript">
Line 9: Line 13:
 
.defs ({
 
.defs ({
 
     Point: {
 
     Point: {
         x: undefined,   //< Number
+
         x: undefined,   //< Number
         y: undefined    //< Number  },
+
         y: undefined    //< Number   
 +
    },
 
     Line: {
 
     Line: {
         start: undefined,   //< Shape.Point
+
         start: undefined,   //< Shape.Point
         end: undefined       //< Shape.Point
+
         end: undefined     //< Shape.Point
 
     },
 
     },
  
Line 25: Line 30:
 
.endType();
 
.endType();
 
</source>
 
</source>
 +
The ''vjo.NEEDS_IMPL'' is a placeholder object in that says that a Function is the value for this element.
 +
 
When you need to refer to an otype definition from another type you must:
 
When you need to refer to an otype definition from another type you must:
 
+
* Use the fully qualified name of the type, or use the ''.needs'' Comment, which serves as a shortcut to the ''otype'' namespace.
Use the fully qualified name of the type, or use the ''.needs'' Comment, which serves as a shortcut to the ''otype'' namespace.
+
* Do not include the ''otype'' in a ''.needs'' section; ''otypes'' are not true types, but are used to provide a namespace for custom types.
 
+
Do not include the ''otype'' in a ''.needs'' section; ''otypes'' are not true types, but are used to provide a namespace for custom types.
+
  
 
The following example:
 
The following example:
 
+
* Uses the ''needs'' Comment to create a shortcut for the ''otype namespace'', ''samples.Shape''
Uses the ''needs'' Comment to create a shortcut for the ''otype namespace'', ''samples.Shape''.
+
* Declares two ''Point'' properties and a ''Line'' property
 
+
* Declares a function typed as ''SimpleLine''
Declares two ''Point'' properties and a ''Line'' property
+
 
+
Declares a function typed as ''SimpleLine''
+
  
 
<source lang="javascript">
 
<source lang="javascript">
Line 44: Line 46:
 
.protos({
 
.protos({
 
     point1 : {x:0, y:0},    //< Shape::Point
 
     point1 : {x:0, y:0},    //< Shape::Point
     point2 : {x:10, y:0},     //< Shape::Point
+
     point2 : {x:10, y:0},   //< Shape::Point
     line1 : undefined,         //< Shape::Line
+
     line1 : undefined,       //< Shape::Line
  
 
     //> public Shape.SimpleLine createLine()
 
     //> public Shape.SimpleLine createLine()

Revision as of 20:22, 14 February 2013

JavaScript object literals provide a way to define and create new objects.  They are also frequently used to define data structures and lightweight class instances.

Object Literals and Function References are frequently used constructs in JavaScript; they can be assigned to variables and properties, returned by functions, and passed as arguments.

In this example:

  • Point is comprised of two Number properties
  • Line is comprised of two Point properties
  • Define the function SimpleLine that takes two Point arguments
  • Define two function references: createLine and isSingularity
vjo.otype('Shape')
.defs ({
    Point: {
        x: undefined,   //< Number
        y: undefined    //< Number   
    },
    Line: {
        start: undefined,   //< Shape.Point
        end: undefined      //< Shape.Point
    },
 
    //> public Shape.Line createLine(Shape::Point pt1, Shape::Point pt2)
    createLine : vjo.NEEDS_IMPL,
 
    //> public boolean isSingularity(Shape.Line line)
    isSingularity: vjo.NEEDS_IMPL
 
})
.endType();

The vjo.NEEDS_IMPL is a placeholder object in that says that a Function is the value for this element.

When you need to refer to an otype definition from another type you must:

  • Use the fully qualified name of the type, or use the .needs Comment, which serves as a shortcut to the otype namespace.
  • Do not include the otype in a .needs section; otypes are not true types, but are used to provide a namespace for custom types.

The following example:

  • Uses the needs Comment to create a shortcut for the otype namespace, samples.Shape
  • Declares two Point properties and a Line property
  • Declares a function typed as SimpleLine
vjo.ctype('samples.ConstructShape')
//> needs samples.Shape
.protos({
    point1 : {x:0, y:0},     //< Shape::Point
    point2 : {x:10, y:0},    //< Shape::Point
    line1 : undefined,       //< Shape::Line
 
    //> public Shape.SimpleLine createLine()
    createLine : function(pt1, pt2) {
        this.line1 = {start: pt1, end: pt2} ;
    }
})
.endType();

Back to the top