·  JavaScript – Syntax – Terms – Prototype

 

- Info:

  - JavaScript is prototype-based language which means that there is no abstract concept of a "class".

  - In a prototype language, every object (instance) is based on some other object called it's prototype.

  - In prototype languages object (instance, child) inherits directly from other object (instance, parent, prototype).

 

- Prototype succession:

  - When you use child to reference some property, engine first looks if child has this property.

  - If it deosn't then it looks if the parent (prototype) has the property.

  - Since parent can have it's own parent (prototype) this continues until property is found or not.

  - This is called prototype succession.

 

- Additional Tutorials:

  - Inheritance

 

- Example:

  - Create D\Temp\JavaScript\Test.html

 

    Test.html

    <script>

   

      //__proto__

      Person   = { name  :"John" , age :25  }; //Typical Person representative.

      Soldier  = { weapon:"rifle", ammo:100 }; //Typical extension of Soldier compared to Person.

      Soldier.__proto__ = Person;              //Person becomes Soldier's prototype

      document.write(Soldier.name);            //Soldier can now access Person's properties

   

      //Object.create(parent)

      Person2  = { name  :"Jill" , age :25 };

      Soldier2 = Object.create( Person2 );     //Person2 becomes Soldier2's prototype

      Soldier2.weapon = "rifle";

      Soldier2.ammo   = 100;

      document.write(Soldier2.name);           //Soldier2 can now access Person2's properties

   

      //new Person()

      function Person3(name, age) {

        this.name = name;

        this.age  = age;

      }

      Soldier3 = new Person3("Jack", 25);      //Create object.

      Soldier3.weapon = "rifle";

      Soldier3.ammo   = 100;

      document.write(Soldier3.name);           //Soldier3 can now access Person3's properties

     

    </script>

 

- Features:

  - Creation by copying.

  - Creating new objects from prototypes is accomplished by a simple operation, copying (like biological cloning).

  - Creating new objects from classes is accomplished by complex operation, instantiation, which includes the

    interpretation of format information in a class.

  - Instantiation is similar to building a house from a plan.

  - Copying appeals to us as a simpler metaphor than instantiation.

 

  - Examples of preexisting modules.

  - Prototypes are more concrete than classes because they are examples of objects rather than descriptions of format and

    initialization.

  - These examples may help users to reuse modules by making them easier to understand.

  - A prototype-based system allows the user to examine a typical representative rather than requiring him to make sense

    out of its description.

 

  - Support for one-of-a-kind objects.

  - Self provides a framework that can easily include one-of-a-kind objects with their own behavior.

  - Since each object has named slots, and slots can hold state or behavior, any object can have unique slots or behavior.   - Class-based systems are designed for situations where there are many objects with the same behavior.

  - There is no linguistic support for an object to possess its own unique behavior, and it is awkward (think Singleton) to

    create a class that is guaranteed to have only one instance. Self suffers from neither of these disadvantages.

  - Any object can be customized with its own behavior.

  - A unique object can hold the unique behavior, and a separate "instance" is not needed.

 

  - Elimination of meta-regress.

  - No object in a class-based system can be self-sufficient; another object (its class) is needed to express its structure

    and behavior.

  - This leads to a conceptually infinite meta-regress: a point is an instance of class Point, which is an instance of

    metaclass Point, which is an instance of metametaclass Point, ad infinitum.

  - On the other hand, in prototype-based systems an object can include its own behavior; no other object is needed to

    breathe life into it. Prototypes eliminate meta-regress.

  - Self is probably the first language to implement prototypes.

    (It also pioneered other interesting technologies like JIT, which later made its way to the JVM.

    So reading the other Self papers should also be instructive).