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.