Objects (Lava.Object)

At the heart of Lava is the object. An object in lava is a structure with properties and behaviors that can be bound to the DOM. A Lava object is basically a normal javascript object with additional functionality. You define an object by calling Lava.Object() and passing a javascript object as an argument like so:

var Car = Lava.Object({
    make: 'Ford',
    model: 'Mustang',
    year: 2013             
});

Now, to create other instances of the Car object, you simply call the clone() method.

var myCar = Car.clone();

The myCar object now has all of the properties, values, and behavior that the Car object contains. You can also pass additional properties to the new object, or even overwrite existing properties.

var myCar = Car.clone({
    color: 'blue',
    year: 2011                
});

Adding additional properties or behavior to an existing Lava object is made trivial through the use of the extend() method.

myCar.extend({
    color: 'blue'               
});

Along with properties, Lava objects can contain behaviors. For example, let's say that we needed a way to obtain a summary of information about the car. This can be done easily by adding a method to the object to return a summary.

myCar.extend({
    summary: function () {
        return myCar.make + '-' + myCar.model + '-' + myCar.year + '-' + myCar.color;      
    }      
});

Setting or getting properties on an object works exactly how you would expect.

// set a property
myCar.color = 'red';

// get a property
var make = myCar.make;
var summary = myCar.summary();

Last edited Sep 11, 2013 at 10:13 PM by tjscience, version 9

Comments

No comments yet.