CSC/ECE 517 Fall 2014/ch1b 30 cs

From Expertiza_Wiki
Revision as of 02:36, 3 October 2014 by Csimmon2 (talk | contribs) (Created page with "== Prototype-based Programming == Most people are familiar with class-based languages, in which to create an object, one defines a ''class'' containing functions and variables, a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Prototype-based Programming

Most people are familiar with class-based languages, in which to create an object, one defines a class containing functions and variables, and then objects are instantiated from the class. Prototype-based programming is an alternative approach to creating objects, where instead of a class, they get their properties from other objects<ref name="moz" />.

As an example, consider creating an object in a class-based approach. We would define a Class that contains the variable var1 and function func1(). Then within the class we would define a constructor, which will create Object which can access func1() and may have a value assigned to var1. Our object is limited to these two properties which we defined in Class, so if we wanted to have Object.var2 as well, we would need to define a new subclass and create objects from the subclass. On the other hand, in a prototype-based approach, we would just define a constructor for the object, which has a prototype containing var1 and func1(). The constructor gives us a new object that has the same properties as the prototype. But in this case, if we want to have Object.var2 or Object.func2(), we can simply declare them after object creation because we are not bound by a class definition.

<references> <ref name="moz">Mozilla Developer Network, "Details of the Object Model"</ref> </references>