CSC/ECE 517 Fall 2014/ch1b 30 cs: Difference between revisions
No edit summary |
No edit summary |
||
Line 6: | Line 6: | ||
== Benefits and Drawbacks == | == 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" />. Prototypes offer more flexibility through the ability to add, modify or remove properties from objects at run time, without needing separate classes for each variation. Thus, creating clones or descendants of | 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" />. Prototypes offer more flexibility through the ability to add, modify or remove properties from objects at run time, without needing separate classes for each variation. Thus, creating clones or descendants of objects can be accomplished with less code. However, you give up the benefits of static typing which might make your code more error prone. | ||
Examples of class-based languages are Java and C++. The most popular prototype-based language is JavaScript, but some other examples are Io<ref name="io" />, REBOL<ref name="rebol" /> and Self<ref name="self" />. | |||
== Prototypes in JavaScript == | == Prototypes in JavaScript == | ||
JavaScript is | Although JavaScript is a prototype-based language, its syntax can give the appearance of being class-based, e.g. <pre>var james = new Student("James Jones", 200031416);</pre> However, this does ''not'' create an object of a <tt>Student</tt> class<ref name="pivotal" />. Consider our constructor function: | ||
<pre> | <pre> | ||
function Student(name, id) { | function Student(name, id) { | ||
Line 31: | Line 33: | ||
== Prototypes in Ruby == | == Prototypes in Ruby == | ||
<ref name="rubyproto" /> | |||
== References == | == References == | ||
Line 37: | Line 40: | ||
<ref name="shah">[http://aaditmshah.github.io/why-prototypal-inheritance-matters/ Aadit Shah, "Why Prototypal Inheritance Matters"]</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> | <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> | ||
<ref name="io">[http://iolanguage.org/ Io]</ref> | |||
<ref name="rebol">[http://www.rebol.com/docs/core23/rebolcore-10.html REBOL - Objects]</ref> | |||
<ref name="self">[http://selflanguage.org/ Self]</ref> | |||
<ref name="pivotal">[http://pivotallabs.com/javascript-constructors-prototypes-and-the-new-keyword/ Pivotal Labs, "JavaScript constructors, prototypes, and the `new` keyword"]</ref> | <ref name="pivotal">[http://pivotallabs.com/javascript-constructors-prototypes-and-the-new-keyword/ Pivotal Labs, "JavaScript constructors, prototypes, and the `new` keyword"]</ref> | ||
<ref name="rubyproto">[https://practicingruby.com/articles/adventure-in-prototypes Avdi Grimm, "An Adventure in Prototypes"]</ref> | |||
</references> | </references> |
Revision as of 00:28, 14 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" />. Prototypes offer more flexibility through the ability to add, modify or remove properties from objects at run time, without needing separate classes for each variation. Thus, creating clones or descendants of objects can be accomplished with less code. However, you give up the benefits of static typing which might make your code more error prone.
Examples of class-based languages are Java and C++. The most popular prototype-based language is JavaScript, but some other examples are Io<ref name="io" />, REBOL<ref name="rebol" /> and Self<ref name="self" />.
Prototypes in JavaScript
Although JavaScript is a prototype-based language, its syntax can give the appearance of being class-based, e.g.
var james = new Student("James Jones", 200031416);
However, this does not create an object of a Student class<ref name="pivotal" />. Consider our constructor function:
function Student(name, id) { this.ID = id; this.Name = name; this.AddToCourse = function(course) { // a function for the object }; }
Note that there is nothing special about this function that makes it a "constructor" function. Instead, we use the new keyword before the function call to signify that a new object is being created. The james.constructor property of our new object is set to the Student function, and then the function runs while treating this as the new object.
Functions in JavaScript also have a prototype property, which is just a regular, initially empty object. If we try to access a property that the object does not have, then JavaScript will look to the object's constructor's prototype. For example:
Student.prototype.SemesterCreditLimit = 13; console.log(james.SemesterCreditLimit); // ==> 13 delete Student.prototype.SemesterCreditLimit; console.log(james.SemesterCreditLimit); // ==> undefined
Prototypes in Ruby
<ref name="rubyproto" />
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> <ref name="io">Io</ref> <ref name="rebol">REBOL - Objects</ref> <ref name="self">Self</ref> <ref name="pivotal">Pivotal Labs, "JavaScript constructors, prototypes, and the `new` keyword"</ref> <ref name="rubyproto">Avdi Grimm, "An Adventure in Prototypes"</ref> </references>