Creating Objects
There are two main ways of creating js-type objects: as anonymous objects, and through constructors such as the new operator.
Creating anonymous objects
- Anonymous (or 'literal') objects in HFL are created the same way as in javascript.
-
{ [<obj-field-def> [, <obj-field-def>]*] }where:<obj-field-def>:=<key> : <value>
- Creates a new anonymous object (or 'object literal') containing the field definitions, if any.
- The <key> values must be unquoted literals that are valid as property names.
- The <value> values can be any valid expression, such as a constant, an expression, or a function definition.
- EXAMPLES:
-
var person = {
name: 'Joe Blow',
age: 45,
occupation: 'Plumber'
};
// Prints "Joe Blow is 45 years old and is a Plumber"
print(person.name+" is "+person.age+" years old and is a "+person.occupation);
var place = {};
place.country = 'France';
place.city = 'Paris';
// Prints "Paris is in France"
print(place.city+" is in "+place.country);
Creating objects using the 'new' operator
- Objects in HFL are created the same way as in javascript.
-
- Creates a new object of the type defined by the <constructor> function.
- EXAMPLES:
-
// Create a new generic object
var myobj = new Object();
// Constructor function for object 'Pet'
function Pet(type, name) {
this.type = type;
this.name = name;
}
// Create three 'Pet' objects
pet1 = new Pet('Dog', 'Fido');
pet2 = new Pet('Donkey', 'Hodey');
pet3 = new Pet('Kitten', 'Kaboodle');
// Prints "My first pet is a Dog named 'Fido'"
print("My first pet is a "+pet1.type+" named '"+pet1.name+"'");