Skip to content
Jing Lu edited this page May 23, 2013 · 24 revisions

All the properties belonging to an object will be available to another object, if the object is the prototype of the latter.

object A             object B
  +- a: 10             +- prototype: object A

A.a == 10      // true
B.a == 10      // true

The property 'a' does not exist in B, but it is also available to B since A is prototype of B.

Prototype Chain

An object can be prototype of another object, even this object has also its prototype, that is prototype chain.

object A               object B               object C               object D
  +- a: 10               +- prototype: A        +- prototype: B        +- prototype: C

When a property of object to be accessed, ReoScript run-time will firstly find the property in its owner object, if not found, then find it in its prototype, and so on, until there is no prototype anymore.

console.log(A.a + ' ' + B.a + ' ' + C.a + ' ' + D.a);

The output is:

10 10 10 10

If we do B.a = 20, the result will be:

10 20 20 20

All functions has prototype

When a function to be defined, the prototype object of this function will also be created automatically. For example:

function User() {
}

console.log(User.prototype);

The output is:

[object Object]

Notice that User.prototype is not in type User, it is just a normal object.

debug.assert(User.prototype instanceof User);      // false
debug.assert(User.prototype instanceof Object);    // true

Built-in constructor such as Array has also 'prototype' property so typically we could use it to extend for Array:

Array.prototype.add = function(element) {
  this.push(element);
};

var arr = new Array();
arr.push(1);

arr.add(2);         // 'add' redirect to 'push'
console.log(arr);

The output is:

[1, 2]

Understand Constructor

A constructor is physically a normal function. Usually we called it Constructor and keep the first letter of its name to be capitalized in order to specify that it will be used to create new object instance.

function User() {
    console.log('user invoked');
}

Function to be called using new keyword will create a new object instance.

User()                // function call
new User              // create instance before function call
new User(a, b)        // create instance with arguments before function call

The all of lines above are valid syntax to use a function, but notice that they does different things.

Create instance

The code below typically be used to create instance from a constructor.

var usr1 = new User();

The this code to be interpreted, ReoScript run-time performs the following actions:

  1. Create an new object instance
  2. Make prototype of created object to User.prototype
  3. Set this keyword to usr1
  4. Call 'User' function, pass this and function arguments

this keyword in constructor

When a function called by new keyword, the this will point to the instance itself. For example:

function User() {
    this.name = 'guest';
}

var usr1 = new User();
console.log(usr1.name);

The output is:

guest