





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to object-oriented programming in javascript, focusing on three popular design patterns: factory, constructor, and prototype. Learn how to create multiple objects with the same properties and functions using these patterns, and understand the differences between them. Also, discover the benefits of using object patterns and the syntax for each.
Typology: Slides
1 / 9
This page cannot be seen from the preview
Don't miss anything!






1
Object‐oriented programming in JavaScript Factory Pattern CConstructor t t P ttPattern Prototype Pattern Dynamic Prototype Pattern
2
JavaScript has no class definitions. Objects are created based on the language‐defined ObjectObject constructconstruct. To create multiple objects based on a desired object structure, functions written to guide instantiation. http://einstein.etsu.edu/~pittares/CSCI3110/examples/6‐1.htm JavaScript's class‐free object‐oriented structure is a ff requentlyl criticizedi i i d element.l
One way to create multiple objects with the same properties and functions is to use the Factory PatternPattern design patterndesign pattern. Function takes in property values, creates an object, assigns properties and functions to the object, and returns the object. The function becomes an "object factory". http://einstein.etsu.edu/~pittares/CSCI3110/examples/6‐2.htm Use identifiers beginning with capital letters for objects (only) to denote distinction from identifiers used with primitive data types.
4
Another alternative is the Prototype Pattern. With this pattern, memory and resources are used more efficientlyy as multiplep objectsj share resources. The Constructor function body is (typically) null. Properties and functions are assigned to the constructed object's prototype. prototype is a default property within an object that contains properties and methods available to all instances of that objectinstances of that object. This technique allows defining 'default values' for objects and object functions. The object creation call does not specify property values. http://einstein.etsu.edu/~pittares/CSCI3110/examples/6‐4.htm
With the Prototype Pattern, value assignments to the instantiated (or instance) object shadow property values in the prototype (they do notproperty values in the prototype (they do not replace them). (Creating a 'search order'.) delete can be used to remove a property/value pair from an object. http://einstein.etsu.edu/~pittares/CSCI3110/examples/6‐5.htm
5
Factory Pattern and Constructor Pattern Object
Prototype Pattern Prototype Properties Functions
Object Properties
Object Properties
Properties Functions
Object OverriddenProperties
Object OverriddenProperties Functions Functions
Properties Overridden Functions
Properties Overridden Functions
Each object contains a language‐defined function toString which outputs the object as a String. U lUnless overriddenidd ii n our objectbj t i i nstance,t thithi s willill output "[object Object]". http://einstein.etsu.edu/~pittares/CSCI3110/examples/6‐5a.htm This function can/should be overridden based on our desire of how the object should be represented as a string. http://einstein.etsu.edu/~pittares/CSCI3110/examples/6‐5b.htm
7
http://einstein.etsu.edu/~pittares/CSCI3110/examples/6// / / / / ‐8.htm
http://einstein.etsu.edu/~pittares/CSCI3110/examples/6‐9.htm
http://einstein.etsu.edu/~pittares/CSCI3110/examples/6‐10.htm
8
When properties of an object are enumerated, some properties are not listed. Some built in properties are designated as nonSome built in properties are designated as non‐ enumerable. Examples include toString and others. Browsers are inconsistent in their behavior of these non‐enumerable elements. In IE, if you override the non‐enumerable property with your own, your override is still nonoverride is still non ‐enumerableenumerable. In Opera Safari andIn Opera, Safari, and Firefox the behavior varies depending on property. http://einstein.etsu.edu/~pittares/CSCI3110/examples/6‐11.htm
2 notable disadvantages of using the "pure" Prototype Pattern: