Javascript Patterns Flashcards
What is the default value of anything in javascript that has not been defined?
Undefined. null has to be set explicitly
Why do primitive types in js have methods?
Because everything in js can be treated as an object. The js runtime wraps primitive types in an Object wrapper when it is evaluated. e.g. calling the (1).toPrecision(3) method on the number one, will wrap the number in a Number object.
What types are supported by js?
Number, String, Object, Boolean, undefined, null, and Symbol.
In js, integer, short, long, double and float are all represented by what type?
Number
difference between var, let, const
Scope: var is function scoped, let and const are block scoped
Definition and assignment: var can be redefined and re-assigned, let can be re-assigned, but not redefined, const cannot be redefined or re-assigned.
What is meant by “functions are first-class citizens”?
In programming languages, when you are able to pass, return and assign a type, that type is considered to be a first class citizen
Functions are objects and can be assinged to variables, passed into other functions as arguments and also be returned from other functions
Functions inherits from the Object prototype and supports all the operational properties inherent to other entities
Difference between classical and prototypal inheritance?
Objects are abstractions of physical real-world objects.
Classes are a Generalization of an object. In other words, Classes are an abstraction of an object of a real world thing. (Classes, then, are an abstraction of an abstraction of a real-world thing). Since a Class is yet another reference to (or abstraction of) its predecessor, each descendant child Class increases the level of abstraction, thus increasing the level of generalization.
As opposed to Classical Inheritance, Prototypal Inheritance does not deal with increasing layers of abstraction. An Object is either an abstraction of a real-world thing, same as before, or is a direct copy of another Object (in other words, a Prototype). Prototypal inheritance is accomplished through an Object linking mechanism.
This is important! It means that Generalizations are just other Objects. With Classical Inheritance, Generalizations are abstractions of abstractions of abstractions… all the way down to the most recent descendant.
The abstraction level here is never deeper than 1; The only abstraction that occurs with Prototypal Inheritance is the abstraction away from real-world things.
What are prototypes?
A prototype is an abstraction of an object
What is a class?
A generalization of an object
What is an object?
A javascript object is an abstraction of a real-world or physical object.
What is a prototypal chain?
a
Difference between for/in and for/of
Both are used to loop through objects. For in loops through property names, for of loops through property values.
difference between a set and an array?
sets dow’t allow duplicate values
Function expression
//assigned to a variable const someFunct = function(){ //some code here }
//assigned to a property within an object
{
someFunct: function(){//some code here}
}
//lamda -passed in as an argument const someFunct(function(x){//some code here}){ //some code here }
Function declaration
function someFunct(){//some code here}
What are the two ways to write a function
function declaration or function expression
What do you need to keep in mind when declaring function expressions?
function expressions uses anonymous functions that don’t have their own name,and when stored in other variables is subject to the same hoisting rules that apply to variables
What is hoisting?
because var variables are function scoped, the variables are hoisted (moved to the top of the function body) at runtime. Thus, var variables declared within blocks inside the function will be hoisted and have not only block scope but also function scope.
Why do javascript developers declare variables at the top of the function body?
To be more explicit about hoisting, as to avoid confusion and errors
const variables and objects
Although const variables cannot be reassigned, when a const variable points to an object, the object properties can be updated without generating an exception
Difference between anonymous and named function expressions.
Strict anonymous functions are not bound to an identifier, which means that it won’t show up in the callstack trace, making it difficult to debug. Strict Anonymous functions also cannot be used in recursion as without a name they can’t call themselves.
named functions are function expressions that have been bound to an identifier, by adding a name after the function keyword, in which case it will show up in the stack strace when an error occurs.
Two kinds of function expressions
named and anonymous.
Lexical Scope
Lexical Scope is the scoping scheme used in JavaScript where every inner scope has access to the outer scopes.
A closure
A closure is a function that encloses or captures variables from its surrounding environment.
Why should anonymous functions be avoided
Anonymous function expressions are less readable, harder to debug and harder to use in recursion.
What can’t you do with a named function expression?
Call them using their own names, outside of their function bodies. Their own names are used as internal identifiers and can only be used from within their own function bodies.
What advantages do function declarations have over function expressions?
They are named, and you can use that name from outside and from within the function. They are not hoisted as variables but as a whole, which makes them impervious to hoisting problems.
What does the .hasOwnProperty() method do?
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it)
REPL
Read, Evaluate, Print, Loop
ES 6 Native method for merging defaults?
Object.assign({}, details,defaults)
Method for coding fallback values
var someVariable = newValue || defaultValue
MergeDefaults Pattern
function(details,defaults){ for( let props in defaults){ if(defaults.hasOwnProperty(props) && !details[props]){ details[props] = defaults[props] } }
return details
}
3 Ways to merge defaults in javascript
- Use fallback values
- ES 6 method - Object.assign(details,defaults)
- Use the mergeDefaults pattern
arity
arity is the amount of arguments a function expects. A function with an arity of 3, expects three arguments.
The arity of a function can be seen by printing functionName.length;
is it possible to call a function without the specified amount of arguments, or no arguments, more arguments than specified?
Yes, however, depending on how those arguments are used in the body of the function, the function might generate an exception or error
What is the arguments object within functions?
Every function has an arguments object, which is an array-like object (not an array) that contains the arguments that was passed into the function at runtime.
One can access these arguments through the arguments object
In which kind of function is the arguments object not found?
ES 6 Arrow functions
How to turn the arguments object into an true array?
- let argsArray = Array.prototype.slice.call(arguments);
2. let argsArray = Array.from(arguments)
What is duck typing?
Javascript cares about what objects can do, not their type
“If it walks like a duck and talks like a duck, then it IS a duck”
Javascript doesn’t care if it is an actual duck as long as the object’s interface behaves like one.
This is related to the concept of polymorphism
How are array-like objects different from arrays?
They look like arrays, but they are not. You can index them, enumerate them and access their length property but the similarity ends there as they don’t inherit from ` Array.prototype’.
How to handle an arbritary amount of arguments in functions?
- By accessing the arguments object and turning it
into an array - ES 6 rest parameters
Difference between rest and spread opperator
Rest Parameter is collecting all remaining elements into an array . Spread Operator is unpacking collected elements such as arrays into single elements