Instantiation Types:
- Polymorphism, Subclasses and prototype Inheritance
- Reviewing most of the instantiation patterns helped me realize how powerful polymorphism can be, particularly when it pertains to managing methods in the prototype chain. To do this before, you would have to go back, change all of your code (functional, functional-shared) to make changes to your methods. But with Psuedoclassical code, we can simply append changes to the methods at any point in time, and it can change the superclass and the subclasses alike, in one spot. But we already knew that ;p.
- Polymorphism refers to the idea that we can design objects to share behaviors (method access) across multiple subclasses in an easy way. However, we have to watch out for the context 'this', and sometimes ensure our context is correct by 'bind'ing our 'this' contexts. Polymorphism = object design to share behaviors.
- Prototype inheritance just refers to prototype chain extension. So if your building a Car superclass and car's generally 'turn on', then it would make sense to share this behavior with all subclasses of Cars, including subclasses like 'mercedes' and 'vw'
- Examples here: http://jsbin.com/vazutoj/edit?js,console
- Recursion Permutations
- So the toy problem was difficult today, but it was important. We had to come up with a function, either with the use of for loops (messy and iffy) or recursion (solid), to express the sequential possibilities of rock paper scissors after 'x' number of rounds.
- While there are a number of different ways to solve this, this is the solution that was handed to us (was disappointed that I couldn't figure it out).
- var rockPaperScissors = function (rounds) {var rpsArray = ['r', 'p', 's'];var results = [];var recurse = function (currentArray) {if(currentArray.length === rounds) {results.push(currentArray);return;}for (var i = 0; i < rpsArray.length; i++) {recurse(currentArray.concat(
rpsArray[i])); }};recurse([]);//Concat results to match expected formatif(results.length > 1) {for(var i = 0; i < results.length; i++){results[i] = results[i].reduce(function(a, b) {return a + b;}, '');}}return results;}; - The first part simply establishes an set array of possibilities. This rpsArray is important to the function because the set range of possibilities remains constant. It is either 'rock' or 'paper' or 'scissors'. We also establish an array of results, whereby we'll push each permutation.
- The second part is our recursive function. Our base case is if the length of the currentArray is equal to the rounds, then we will return that specific solution. Our recursive function will iterate through and concate each combination of our rpsArray, then call back on the recurse function. Once a '1', '2', '3' etc digit number is reached, it will push that into our results array.
- The third part is a simple concat function that combines our results, making them into strings using a reduce method.
- Even after reviewing it, it's difficult for me to understand. I think recursion is one of those things that takes a lot of practice to get used to. Also, I'm not so sure that some of the people who figured it out used outside sources to help them, or if they really did figure it out on their own. If they figured it out on their own, I'd love to see the code!
- Object.create() vs new
- Bird.prototype = Animal.prototype;
- all changes you make to the latter will also be reflected in
Animal
and all other classes that inherit from it. not good! - Bird.prototype = new Animal();
- animal requires arguments, which you may not want to pass any into and invoking animal may have unintended side effects, like increasing a counter instance.
- Bird.prototype = Object.create(Animal.prototype);
- No real drawbacks. This is what we should use.
- setInterval vs. setTimeout
setTimeout(expression, timeout);
runs the code/function once after the timeout.setInterval(expression, timeout);
runs the code/function in intervals, with the length of the timeout between them.- There is a significant performance discrepency between the two. Only use setInterval for minute or limited tasks, as this can really bog down your rig if used incorrectly.
No comments:
Post a Comment