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.
Difference between revisions of "VJET/Typing Object Literals and Function References"
< VJET
m (refine examples) |
|||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
− | 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. | + | 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. | + | 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: | In this example: | ||
− | * Point is comprised of two ''Number'' properties | + | |
− | * Line is comprised of two ''Point'' properties | + | *Point is comprised of two ''Number'' properties |
− | * Define the function ''SimpleLine'' that takes two ''Point'' arguments | + | *Line is comprised of two ''Point'' properties |
− | * Define two function references: ''createLine'' and ''isSingularity'' | + | *Define the function ''SimpleLine'' that takes two ''Point'' arguments |
+ | *Define two function references: ''createLine'' and ''isSingularity'' | ||
<source lang="javascript"> | <source lang="javascript"> | ||
Line 17: | Line 18: | ||
}, | }, | ||
Line: { | Line: { | ||
− | start: undefined, //< Shape | + | start: undefined, //< Shape:Point |
− | end: undefined //< Shape | + | end: undefined //< Shape:Point |
}, | }, | ||
− | //> public Shape.Line createLine(Shape | + | //> public Shape.Line createLine(Shape:Point pt1, Shape:Point pt2) |
createLine : vjo.NEEDS_IMPL, | createLine : vjo.NEEDS_IMPL, | ||
− | //> public boolean isSingularity(Shape | + | //> public boolean isSingularity(Shape:Line line) |
isSingularity: vjo.NEEDS_IMPL | isSingularity: vjo.NEEDS_IMPL | ||
}) | }) | ||
.endType(); | .endType(); | ||
− | </source> | + | </source> The ''vjo.NEEDS_IMPL'' is a placeholder object in that says that a Function is the value for this element. |
− | 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: |
− | + | ||
− | + | ||
− | The following example: | + | *Use the fully qualified name of the type, or use the ''.needs'' Comment, which serves as a shortcut to the ''otype'' namespace. |
− | * Uses the ''needs'' Comment to create a shortcut for the ''otype namespace'', ''Shape'' | + | *Do not include the ''otype'' in a ''.needs'' section; ''otypes'' are not true types, but are used to provide a namespace for custom types. |
− | * Declares two ''Point'' properties and a ''Line'' property | + | |
− | * Declares a function that takes two ''Point'''s and returns a ''Line'' | + | The following example: |
− | * Declares a function that take a ''Line'' and returns a boolean | + | |
+ | *Uses the ''needs'' Comment to create a shortcut for the ''otype namespace'', ''Shape'' | ||
+ | *Declares two ''Point'' properties and a ''Line'' property | ||
+ | *Declares a function that takes two ''Point'''s and returns a ''Line'' | ||
+ | *Declares a function that take a ''Line'' and returns a boolean | ||
<source lang="javascript"> | <source lang="javascript"> | ||
Line 45: | Line 47: | ||
//> needs Shape | //> needs Shape | ||
.protos({ | .protos({ | ||
− | point1 : {x:0, y:0}, //< Shape | + | point1 : {x:0, y:0}, //< Shape:Point |
− | point2 : {x:10, y:0}, //< Shape | + | point2 : {x:10, y:0}, //< Shape:Point |
− | //> public Shape.Line createLine(Shape | + | //> public Shape.Line createLine(Shape:Point pt1, Shape:Point pt2) |
createLine : function(pt1, pt2) { | createLine : function(pt1, pt2) { | ||
this.point1 = pt1 ; | this.point1 = pt1 ; | ||
Line 55: | Line 57: | ||
}, | }, | ||
− | //> public boolean isSingularity(Shape | + | //> public boolean isSingularity(Shape:Line line) |
isSingularity: function(line) { | isSingularity: function(line) { | ||
return line.start.x == 0 | return line.start.x == 0 | ||
Line 64: | Line 66: | ||
}) | }) | ||
.endType(); | .endType(); | ||
− | </source> | + | </source> |
− | It is important to note that we can use our VJET types in freeform Javascript as well | + | It is important to note that we can use our VJET types in freeform Javascript as well <source lang="javascript"> |
− | <source lang="javascript"> | + | //> Shape:Point |
− | //> Shape | + | |
var point1 = {x:0, y: 0}; | var point1 = {x:0, y: 0}; | ||
− | //> Shape | + | //> Shape:Point |
var point2 = {x:10, y:20}; | var point2 = {x:10, y:20}; | ||
− | //> Shape.Line createLine(Shape | + | //> Shape.Line createLine(Shape:Point pt1, Shape:Point pt2) |
function createLine(pt1, pt2) { | function createLine(pt1, pt2) { | ||
point1 = pt1 ; | point1 = pt1 ; | ||
Line 81: | Line 82: | ||
} | } | ||
− | //> boolean isSingularity(Shape | + | //> boolean isSingularity(Shape:Line line) |
function isSingularity(line) { | function isSingularity(line) { | ||
return line.start.x == 0 | return line.start.x == 0 | ||
Line 96: | Line 97: | ||
out("line1 singularity: " + isSingularity(line1)) ; | out("line1 singularity: " + isSingularity(line1)) ; | ||
out("line2 singularity: " + isSingularity(line2)) ; | out("line2 singularity: " + isSingularity(line2)) ; | ||
− | </source> | + | </source> |
− | [[Category:VJET]] | + | [[Category:VJET]] [[Category:VJO]] |
− | [[Category:VJO]] | + |
Latest revision as of 15:54, 20 March 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();
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, Shape
- Declares two Point properties and a Line property
- Declares a function that takes two Point's and returns a Line
- Declares a function that take a Line and returns a boolean
vjo.ctype('MyShape') //> needs Shape .protos({ point1 : {x:0, y:0}, //< Shape:Point point2 : {x:10, y:0}, //< Shape:Point //> public Shape.Line createLine(Shape:Point pt1, Shape:Point pt2) createLine : function(pt1, pt2) { this.point1 = pt1 ; this.point2 = pt2 ; return {start: pt1, end: pt2} ; }, //> public boolean isSingularity(Shape:Line line) isSingularity: function(line) { return line.start.x == 0 && line.start.y == 0 && line.end.x == 0 && line.end.y == 0 ; } }) .endType();
//> Shape:Point var point1 = {x:0, y: 0}; //> Shape:Point var point2 = {x:10, y:20}; //> Shape.Line createLine(Shape:Point pt1, Shape:Point pt2) function createLine(pt1, pt2) { point1 = pt1 ; point2 = pt2 ; return {start: pt1, end: pt2}; } //> boolean isSingularity(Shape:Line line) function isSingularity(line) { return line.start.x == 0 && line.start.y == 0 && line.end.x == 0 && line.end.y == 0 } var line1 = createLine(point1, point2) ; var line2 = createLine(point1, point1) ; // is the singularity var out = vjo.sysout.println ; // Will output to the Eclipse console out("line1 singularity: " + isSingularity(line1)) ; out("line2 singularity: " + isSingularity(line2)) ;