Create Objects

Object Literals

The most basic way to create an object is to use the object literal syntax. An object literal consists of a comma-delimited list of property name:value pairs enclosed in braces. For example, the following code creates an object containing information about a customer:

var customer={
    name:"John Doe",
    address:"1311 Maple Drive",
    city:"New York",
    state:"NY",
    zipcode:12345,
    phonenumber:"212-555-1234"  
};

The order in which you specify properties is unimportant. In fact, DScript stores the properties in alphabetical order based on the property name. This organization allows DScript to more efficiently access the properties later.

New Operator

A more common way to create an object is to use the New operator and an object constructor. For example, the following code creates an empty object using the built-in object constructor named Object:

var customer=new Object();

See new.

See Object.

Once you create an object, you can add properties as follows

customer.name="John Doe";
customer.address="1311 Maple Drive";
customer.city="New York";
customer.state="NY";
customer.zipcode=12345;
customer.phonenumber="212-555-1234";

It is important to note that you create a new property simply by assigning a value to it. In the example above, the object customer initially has no properties. However, executing the statement

customer.name="John Doe";

both creates a new property AND assigns a value to it. If the property already exists, the statement above simply alters the property value.

Object Constructors

An object constructor is a function you call using the new operator that sets default property values. In the example above, an empty object was created using the built-in object constructor named Object. A more common way to create an object is to define and use your own constructor. The following constructor accepts the customer name as an argument and assigns it to the appropriate property. The constructor also sets default values for all other properties.

Customer(name):={
    this.name=name;
    this.address="";
    this.city="";
    this.state="";
    this.zipcode=0;
    this.phonenumber="";
}

Note the object constructor uses the keyword thisto refer to the object being constructed. DScript allows a shorthand notation that can simplify the definition. Whenever you reference a property but give no object name, the object is assumed to be this, that is,

.name is identical to this.name Therefore, you can define the Customer object constructor as Customer(name):={ .name=name; .address=""; .city=""; .state=""; .zipcode=0; .phonenumber=""; }

Also note the constructor does not return the object; instead, it just initializes the object. The operator New is responsible for creating the object and for setting the value of this.

See new.

See this.

Using the constructor above, you can create a customer object as follows:

var customer=new Customer("John Doe");

Typically, when you design a constructor you put all required properties in the constructor's argument list. In the example, only the customer's name is required. All other properties have default values.

Copying Objects

When you copy an object into a variable, DScript creates an entirely new object. For example, the following code creates two objects named ob1 and obj2:

var obj1=new Customer("John Doe");
var obj2=obj1;

If you modify the content of obj2, it does not affect obj1. This is a little different from some implementations of JavaScript where copying an object into a variable just copies a pointer to the original object. Just as with lists, DScript copies and compares objects by value rather than by reference. This is because DScript treats lists and objects as fundamental data types, like numbers and text strings.

Nested Objects

Objects can contain other objects as property values. For example, here is a constructor for an Order object that includes the Customer object as a property:

Order(name):={
    .customer=new Customer(name);
    .date=now;
    .items=[];
}