# Object-Model Architecture

If you as developer come from Object-Oriented backgrounds, you may find the JavaScript Object Model challenging because of its counter-intuitiveness.

However, you can use part of the structure and the [Don't Repeat Yourself (DRY) principles](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) from Object-Oriented languages, and use them in JavaScript development.

## Getting Started

1. [Creating new objects](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/inheritance#creating-new-objects)
2. [Adding constructors](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/inheritance#adding-contructors)
3. [Instantiating new parent objects](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/inheritance#instantiating-new-parent-objects)
4. [Inheriting parent properties](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/inheritance#inheriting-parent-properties)
5. [Understanding parent objects](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/inheritance#understanding-parent-objects)

### Creating New Objects

To create a new object with Kendo UI, define it by calling `kendo.Class.extend`—for example, `var person = kendo.Class.extend({});`. This approach creates a new `person` object to which you can add properties and functions. The approach uses an object literal notation so to separate them from their value, you have to declare variables with a semi-colon (`:`) instead of an equal sign (`=`). Also, the example uses the `this` keyword to reference local variables inside the object. If the context results are not specified, the variable will not be found.

```
var Person = kendo.Class.extend({
    firstName: 'Not Set',
    lastName: 'Not Set',
    isAPrettyCoolPerson: false,
    sayHello: function() {
        alert("Hello! I'm " + this.firstName + " " + this.lastName);
    }
});

var person = new Person();
person.sayHello();
```

**Figure 1: Creating a new object**

![Kendo UI for jQuery Class - Empty Object](https://www.telerik.com/kendo-jquery-ui/documentation/assets/e9cf7f70aaf9870c4f3e01b3aa367a68/empty-object.png)

### Adding Constructors

You can also add a constructor for this object by including an `init` method. To create a new person, build up a new `person` object, such as a new [John Bristowe](http://twitter.com/johnbristowe)), and set `isAPrettyCoolPerson` to `true`.

```
var Person = kendo.Class.extend({
    firstName: 'Not Set',
    lastName: 'Not Set',
    isAPrettyCoolPerson: false,
    init: function(firstName, lastName) {
        if (firstName) this.firstName = firstName;
        if (lastName) this.lastName = lastName;
    },
    sayHello: function() {
        alert("Hello! I'm " + this.firstName + " " + this.lastName);
    }
});

var person = new Person("John", "Bristowe");
person.isAPrettyCoolPerson = true;
person.sayHello();
```

**Figure 2: Setting the name of the new person**

![Kendo UI for jQuery Class - Setting the Name Field](https://www.telerik.com/kendo-jquery-ui/documentation/assets/ca0ec5786a85190ee2d5dfad94c1417b/setting-name-field.png)

### Instantiating New Parent Objects

You can instantiate a new `person` object of type parent that inherits the properties of `person` by extending the base person object and toggle the quality (cool).

```
var Parent = Person.extend({
    firstName: 'Mark',
    lastName: 'Holland'
});

var myDad = new Parent();
myDad.isAPrettyCoolPerson = true;
myDad.sayHello();
alert(myDad.isAPrettyCoolPerson);
```

**Figure 3: Instantiating a Parent**

![Kendo UI for jQuery Class - Create a Parent Object](https://www.telerik.com/kendo-jquery-ui/documentation/assets/eb673c93538221d61526f308859485e4/create-parent-object.png)

**Figure 4: Toggling a quality**

![Kendo UI for jQuery Class - Toggle Boolean](https://www.telerik.com/kendo-jquery-ui/documentation/assets/014ccd9be8734449d7d346383bed429c/toggle-boolean.png)

### Inheriting Parent Properties

A child inherits some, but not all, properties from their parents. If you inherit your father's last name, you can override the first name since you have your own. Also, if your dad is a cool person, you might inherit from him that too.

```
var Child = Parent.extend({});
var me = new Child();
me.firstName = "Burke";
me.sayHello();
alert(me.isPrettyCoolPerson);
```

**Figure 5: Inheriting Some Parent Properties**

![Kendo UI for jQuery Class - Inheriting Parent Properties](https://www.telerik.com/kendo-jquery-ui/documentation/assets/f934605f1c650ef6de97969192ee3385/inherit-parent-properties.png)

### Understanding Parent Objects

It seems the child does not inherit being cool from their parent. Actually, it inherits the trait, which the parent inherited from the `person` object. Because `isACoolPerson` was set after the dad was created, it is specific to his object instance of `parent`. And, by default, parents are not cool people.

## Sample Case

The following example demonstrates how to experiment with the inheritance concept in Kendo UI.

```dojo
<div id="content"></div>

<script>
function log(msg) {
    $("#content").append("<p>" + msg + "</p>");
}

// Create a new person class.
var Person = kendo.Class.extend({
    firstName: 'Not Set',
    lastName: 'Not Set',
    isPrettyCoolPerson: false,
    init: function(firstName, lastName) {
        if (firstName) this.firstName = firstName;
        if (lastName) this.lastName = lastName;
    },
    sayHello: function() {
        log("Hello! I'm " + this.firstName + " " + this.lastName)
    }
});

// Create a new John Bristowe of type person.
var person = new Person("John", "Bristowe");
// Say hello John
person.sayHello();

// Create a parent object of type person.
var Parent = Person.extend({
    firstName: 'Mark',
    lastName: 'Holland'
});

// Create the parent.
var myDad = new Parent();
myDad.isPrettyCoolPerson = true;

// Say hello, parent.
myDad.sayHello();
// Expose the parent's coolness factor.
    log("Cool? " + myDad.isPrettyCoolPerson);

// Create a new child object that inherits from the parent.
var Child = Parent.extend({});

// Create me.
var me = new Child();
me.firstName = "Burke";

// Hello!
me.sayHello();
// Expose my coolness.
log("Cool? " + me.isPrettyCoolPerson);
</script>
```

## See Also

* [Get Started with Kendo UI](https://www.telerik.com/kendo-jquery-ui/documentation/intro/first-steps)
* [Widget Wrapper and Element References](https://www.telerik.com/kendo-jquery-ui/documentation/intro/widget-basics/wrapper-element)
* [Data Attributes Configuration](https://www.telerik.com/kendo-jquery-ui/documentation/intro/widget-basics/mvvm-initialization)
* [Widget Methods and Events](https://www.telerik.com/kendo-jquery-ui/documentation/intro/widget-basics/events-and-methods)
* [Destroy Widgets](https://www.telerik.com/kendo-jquery-ui/documentation/intro/widget-basics/destroy)
