Digging deeper into JS Flashcards Preview

Phase_2 > Digging deeper into JS > Flashcards

Flashcards in Digging deeper into JS Deck (14)
Loading flashcards...
1

tick marks in js syntax, what do they represent. For example: console.log( `this is the end \n and you know it` )

mnemonic: think `literally`, with air quotes!

The tick marks are used in indicate 'template literals'. Template literals are string literals which are used to embed expressions that otherwise would not work within strings.

2

What is a string literal?

A string literal is a string with either single or double quotes. This constitute one of the 6 primitive types in javaScript ( BS NUNS: boolean, string, null, undefined number, symbol ).

3

Describe JS hoisting.

Hoisting is a javaScript mechanism in which all variables and function declarations within a specific scope or lexical environment are automatically pushed to the top as JS reads through ones code.

4

Write an IIFE and explain why they are useful.

An IIFE is an immediately invoked function expression and it is represented by wrapping a function in parentheses with an empty bracket at the end of it to call the function. You can also choose to write your IIFE with the empty bracket on the inside of the parentheses after the function!

( function( ){ /*code here*/ }( ) );
( function( ){ /*code here*/ } )( );

All IIFEs do three things: they create a function instance by declaring and defining the function, they call the function and discard the function!

IIFE's are important because they help prevent polluting of the global environment by variables.

5

What is a function expression? A function declaration? A function statement?

A function expression occurs when we create a function using the var keyword, and a function declaration is when we create a function using the 'function' keyword. A function statement is a pseudonym for function declaration.

6

.

.

7

.

.

8

Define a JS constructor

A constructor function is used to create objects in JS. So you might declare a function and within that function you give it several properties. That function can be called upon using the new keyword to create new objects!

function iConstruct = (name,age,sex) => {
this.name = name;
this.age = age;
this.sex = sex;
};

var student1 = new iConstruct( 'Danny', 26, 'male' );
var student2 = new iConstruct( 'Rita', 24, 'female' );

9

How would one add properties to the prototype of a JS constructor.

Simple. Just use dot notation and the equals sign. If student1 returned the following:
{ name:'Danny', age:20, sex:'female' }
typing in student1.sex = 'male'
changes the sex property.

10

Describe a closure and give an example of when one might use a closure.

https://medium.com/@zfrisch/destroying-buildings-a-simple-guide-to-javascript-closures-ef9fc326c73d

A closure is a function. A FUNCTION. That's First of.

A closure is an inner function which has access to its outer (enclosing) functions variables.

function demo( ) {
var msg = 'I am your closure!'
var num = 0;
return function ( ) {
num++;
console.log('num: ',num)
return msg;
}
}

In the function above ace is our closure.

To access our closure we could call it by writing demo( )( ).

To access our closure we could store the demo( ) in a variable and call that variable to access our closure!

NOW THE CLOSURE TRICK

If you store your enclosing function in a variable in which you call it and then you call that variable something funny happens with our code above - it starts iterating on the num variable!

demo( )( ) // 1
demo( )( ) // 2

But...

var dm = demo( );
dm( ) // 1
dm( ) // 2
dm( ) // 3

11

Describe what the 'this' operator in JS is and give three ways to declare it when calling a function in JS.

The this keyword is javaScripts way of having an explicit reference to the function or scope in which it’s being called or used, or belongs to (my definition). This will always point to the object or function or scope (such as the window object) in which its owned. It’s a representation of javaScripts object oriented design.

1. this is often used in js constructor functions.
2. when writing an object you can choose to include the this keyword to reference other properties in the object.
3. this can also be used when referencing elements declared in the global scope or the window. See showFullName example here => http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/

The global object is our window. So just as we might say var a = 10; we can also say window.a = 10.

12

What is a first class function and what is a higher order function in JS? Write an example of one.

A first class function is any function within JS which can be treated in the same way as an object would, while Higher order functions are callback functions, they are passed into other functions as arguments.

13

Describe the differences between bind, call, apply in javaScript.

Each of these methods relate to the this keyword. Bind is used to set a function as a property of some other object. If we want a function or an object to refer to another object we might type ourFunc.bind(functTwo( )) this would bind the values of funcTwo to ourFunc.

Using call. Call is a function that executes another function while passing to it an object and argujments!
var obj = {num:2};
var addThis = function(a){ return this.num + a + b; }
addThis.call( obj, 3, 4 );

Using apply. Apply is exactly the same as call except instead of a number of arguments we pass an array!
var obj = {num:2};
var arr = [5,6]
var addThis = function(a){ return this.num + a + b ; }
addThis.call( obj, arr );

14

Describe what a pure JS function is?

A pure function is a deterministic ( function returns one and only one response consistently ) function that has no side effects. These functions are easy to reason about, they are obviously predictable and less fragile. If a function can be made into a pure function it should be so.