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