CSC/ECE 517 Fall 2014/ch1b 30 cs: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
(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...")
 
No edit summary
Line 3: Line 3:
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" />.
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 <tt>Class</tt> that contains the variable <tt>var1</tt> and function <tt>func1()</tt>. Then within the class we would define a constructor, which will create <tt>Object</tt> which can access <tt>func1()</tt> and may have a value assigned to <tt>var1</tt>. Our object is limited to these two properties which we defined in <tt>Class</tt>, so if we wanted to have <tt>Object.var2</tt> 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 <tt>var1</tt> and <tt>func1()</tt>. The constructor gives us a new object that has the same properties as the prototype. But in this case, if we want to have <tt>Object.var2</tt> or <tt>Object.func2()</tt>, we can simply declare them after object creation because we are not bound by a class definition.
As an example, consider creating an object in a class-based approach. We would define a <tt>Class</tt> that contains the variable <tt>var1</tt> and function <tt>func1()</tt>. Then within the class we would define a constructor, which will create <tt>Object</tt> which can access <tt>func1()</tt> and may have a value assigned to <tt>var1</tt>. Our object is limited to these two properties which we defined in <tt>Class</tt>, so if we wanted to have <tt>Object.var2</tt> 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 function that creates an object from a ''prototype'' containing <tt>var1</tt> and <tt>func1()</tt>. The constructor gives us a new object that has the same properties as the prototype. But in this case, if we want to have <tt>Object.var2</tt> or <tt>Object.func2()</tt>, we can simply declare them after object creation because we are not bound by a class definition.


== Benefits and Drawbacks ==
Classes and prototypes are commonly seen as not strictly better than the other, but rather just appropriate for different situations.<ref name="shah" /><ref name="se1" />
== References ==
<references>
<references>
<ref name="moz">[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model Mozilla Developer Network, "Details of the Object Model"]</ref>
<ref name="moz">[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model Mozilla Developer Network, "Details of the Object Model"]</ref>
<ref name="shah">[http://aaditmshah.github.io/why-prototypal-inheritance-matters/ Aadit Shah, "Why Prototypal Inheritance Matters"]</ref>
<ref name="se1">[http://programmers.stackexchange.com/questions/110936/what-are-the-advantages-of-prototype-based-oop-over-class-based-oop Programmers Stack Exchange, "What are the advantages of prototype-based OOP over class-based OOP?"]</ref>
</references>
</references>

Revision as of 20:32, 5 October 2014

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 function that creates an object from 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.

Benefits and Drawbacks

Classes and prototypes are commonly seen as not strictly better than the other, but rather just appropriate for different situations.<ref name="shah" /><ref name="se1" />

References

<references> <ref name="moz">Mozilla Developer Network, "Details of the Object Model"</ref> <ref name="shah">Aadit Shah, "Why Prototypal Inheritance Matters"</ref> <ref name="se1">Programmers Stack Exchange, "What are the advantages of prototype-based OOP over class-based OOP?"</ref> </references>