- Basic Data Structures and Instantiation Styles:
- Stacks
- Last in, first out (LiFo)
- Data organized like plates. After washing a plate, you pile it on top of an already existing pile of plates. When you need a plate, you take the one on top. Stack = pile of plates.
- 0 -> []; 1-> [0]; 2 -> [0, 1]; 3 -> [0, 1, 2]; [0, 1, 2, 3] -> 3; [0, 1, 2] -> 2;
- Queue
- First in, first out (FiFo)
- Data organized like people in line at a grocery store. The first ones to enter the line are the first to exist the line.
- 0 -> []; 1 -> [0]; 2 -> [0, 1]; [0, 1, 2] -> 0; [1, 2] -> 1;
- Instantiation patterns (http://callmenick.com/post/instantiation-patterns-in-javascript)
- Confiding into a single pattern with respect to creating a new class object. The realization of a new object with class properties that are shared with others, using a specific style (or pattern).
- Functional Instantiation
- var Func = function () {
- someInstance = {};
- var a = 1;
- someInstance.logA = function () { console.log(1); }
- return someInstance;};
- var myFunc = func();
- func.logA() //1
- Creating an object within a function so that every time the function is called we have methods that are attached to that object.
- Pros: Readability. It's obvious how the code works.
- Cons: This pattern creates a new set of functions in memory for each instance of that function, making it awfully inefficient in enterprise-level software.
- Functional Shared Instantiation
- var Func = function () {
- someInstance = {};
- var a = 1;
- _.extend(someInstance, funcMethods)
- return someInstance; };
- var funcMethods = {};
- funcMethod.logA = console.log(this.a);
- var myFunc = Func();
- someInstance.logA() //1
- Creating a function and an object with methods and then conjoining the two such that the instance created within the function shares the same methods as the outside object.
- Pros: Allows for greater memory management because methods are referenced outside of the function rather than from within itself.
- Cons: If we want to edit the methods at some point in the future, we cannot easily do so because all of the changes to the objects before the change still retain those old methods.
- Prototypal Instantiation
- var Func = function () {
- someInstance = Object.create(funcMethods);
- someInstance.a = 1;
- return someInstance; };
- var funcMethods = {};
- funcMethods.logA = console.log(1);
- var myFunc = Func();
- myFunc.logA() //1
- Despite its name, this instantiation doesn't actually utilize the prototype keyword. Rather, it attaches the methods from the Object.create(*obj*) to the prototype chain of 'someInstance'.
- Pros: Allows for you to change the methods in funcMethods easily, since all you have to do is change the source obj (funcMethods) in the prototype chain.
- Cons: Apparently, not many, though this instantiation pattern is not as idiomatic and the psuedoClassical.
- Psuedo-Classical Instantiation
- var Func = function() {
- this.a = 1;};
- Func.prototype.logA = function () { console.log(1) };
- var myFunc = new Func();
- myFunc.logA() // 1
- Instances are created with the 'new' keyword rather than the Object.create() method. Though it retains the same functionality as the prototypal instantiation (utilizes prototype-chains to keep methods attached to realized object classes)
- Pros: Less code and apparently the most efficient instantiation pattern. Its also the most idiomatic and is often used in enterprise-level code.
- Cons: None, its perfect unless you have no idea how 'this' is utilized.
References:
https://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/Stacks%20and%20Queues.html
http://stackoverflow.com/questions/10974922/what-is-the-basic-difference-between-stack-and-queue
http://www.ryanatkinson.io/javascript-instantiation-patterns/
https://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/Stacks%20and%20Queues.html
http://stackoverflow.com/questions/10974922/what-is-the-basic-difference-between-stack-and-queue
http://www.ryanatkinson.io/javascript-instantiation-patterns/
No comments:
Post a Comment