First of all, when I think of reflections, I think of this
ASSIGNMENT
Watch this video
Questions for reflection (please reflect on your blog and discuss in comments below):
- 1. Why do languages provide the switch statement, when we can achieve the same thing with multiple if… elseif statements? Show one example of how you might use the switch statement.
A: To be honest, I haven’t found a good explanation for why ‘switch’ was originally implemented (other than it is in C++), but I have found plenty of examples where it is praised as “simple to use, logical in syntax, and most importantly, many programmers swear by it”1 . So there are plenty of examples for why it stuck around.
Example:switch (today){ case"sunday": alert("You watch TV, it's Sunday"); [break;] case"monday": alert("Who's got a case of the Mondays?"); [break;] case"tuesday": alert("It's only Tuesday, STFU!"); [break;] case"wednesday": alert("Humpday"); [break;] case"thursday": alert("Tonight is the best night on NBC"); [break;] case"friday": alert("Rally and peak at 9pm, bed by 11"); [break;] case"saturday": alert("Saturday, it's a Saturday"); [break;] default: alert("Your society has no word for today's name!"); [break;] } - 2. What is encapsulation, and what do functions encapsulate?
A: Encapsulation: “every class inherits the methods of its parent and only needs to define things it wishes to change” or “A Class defines only the characteristics of the Object, a method defines only how the method executes.”2
Functions encapsulate local variables. - 3. What is a pure function? Is the function show() provided in Eloquent Javascript a pure function?
A: Pure functions are function that “have no memory or I/O side effects”3 Side effects mean “in addition to producing a value, it also modifies some state or has an observable interaction with calling functions or the outside world.”4 So basically a pure function is basic mathematics, 2 + 2 = 4, always. And no variables are affected
I don’t think the show() is a pure function because it does the bidding of the function associated within it, therefore it does change variables and not do a basic math function. - 4. What do we mean when we say a variable in a function is shadowing a top level variable?
A: A top level variable is a variable created outside functions that can be accessed by ALL functions. When a function calls a variable, it first looks for it locally (aka inside this function) and if not found it then looks outside the function to the top level variables. I guess it is a shadow because top level variables are out there and obvious, while a variable with the same name is hiding in the shadows of the function waiting to pounce when it is needed. - 5. A recursive function must have some sort of an end condition. Why would we get a “out of stack space” error message if a recursive function does not have an end condition?
“To understand recursion you must first understand recursion”5
A: A recursive function is a function that calls itself inside itself. Without an end statement, it would continue on forever. The “stack” is where the web browser stores the variables as it works thru functions, each browser has a limited amount of space, thus, if a function runs on forever and continues working thru variables, it will run out of space in the stack. - 6. Reflect about the difference between object inheritance and class inheritance
A: Object inheritance allows you to basically make a copy of an object that you expand upon. Class inheritance means that you need to define a new class tat extends a previous class and then create objects of that class.6 - 7. What is object augmentation, and how do we do it?
A: Object Augmentation: Take an existing object and add new stuff to it at anytime. To do it, simply assign it. The general pattern is myObject[name] = value;
Just do it! - 8. There is a way to add a method to String, such as any new String we create will have that augmented method (this is a bit different from object augmentation). How would you do this?
A:String.prototype.newMethod = function (foo, bar) { //put code for new String function here } - 9. What is garbage collection?
A: According to the video, it’s “mark and sweep so cycles do get reclaimed”, which meant almost nothing to me, but the internets told me it is basically the “collector” flags each block of data or variable that the program can reach, it then deletes any blocks without flags because they are not being used. - 10. What is the difference between an array and an object?
A: Array inherits from Object. Arrays have a length, and objects do not. The video recommends that you use objects when the names are arbitrary strings and use arrays when the names are sequential integers.
Exercises 3.1 – 3.2 from chapter 3
- 3.1 – Write a function called absolute, which returns the absolute value of the number it is given as its argument. The absolute value of a negative number is the positive version of that same number, and the absolute value of a positive number (or zero) is that number itself.
function absolute(arg){ if (arg < 0 ){ return -arg } else { return arg } }I then used buttons to open alerts for different numbers
See an example here. - 3.2 - Write a function greaterThan, which takes one argument, a number, and returns a function that represents a test. When this returned function is called with a single number as argument, it returns a boolean: true if the given number is greater than the number that was used to create the test function, and false otherwise.
function greaterThan(x) { return function(y) { return y > x; }; } var greaterThanTen = greaterThan(10); alert(greaterThanTen(9));See it here
#p2pu-Jan2011-javascript101