stuff Flashcards

1
Q

What are Immediately-invoked Function Expressions (IIFE)?

A

Immediately-invoked Function Expressions are a way to execute functions immediately, as soon as they are created.

  • don’t pollute the global object
  • simple way to isolate variables declarations
  • Function declarations want a name, while function expressions do not require it.
  • An IIFE can also be named regular functions (not arrow functions). This does not change the fact that the function does not “leak” to the global scope, and it cannot be invoked again after its execution
(function() {
  /* */
})()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Immediately-invoked Function Expressions (IIFE) syntax

A
(function() {
  /* */
})()
(() => {
  /* */
})()

You could also put the invoking parentheses inside the expression parentheses:

(function() {
  /* */
}())
(() => {
  /* */
}())

We have a function defined inside parentheses, and then we append () to execute that function: (/* function */)().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why do Immediately-invoked Function Expressions require wrapping parentheses?

A

Those wrapping parentheses are actually what make our function be considered an expression. Otherwise, the function declaration would be invalid, because we didn’t specify any name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why IIFEs might start with a semicolon?

;(function() {
  /* */
})()
A

This prevents issues when blindly concatenating two JavaScript files. Since JavaScript does not require semicolons, you might concatenate with a file with some statements in its last line that causes a syntax error.

This problem is essentially solved with code bundlers like webpack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Closures

A

Closures are important because they control what is and isn’t in scope in a particular function, along with which variables are shared between sibling functions in the same containing scope.

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

JavaScript variables can belong to the local or global scope.

Global variables can be made local (private) with closures.

A local variable can only be used inside the function where it is defined. It is hidden from other functions and other scripting code.

Global and local variables with the same name are different variables. Modifying one, does not modify the other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

var let const

A

var: The scope of a variable defined with the keyword “var” is limited to the “function” within which it is defined. If it is defined outside any function, the scope of the variable is global.
var is “function scoped”.

let: The scope of a variable defined with the keyword “let” or “const” is limited to the “block” defined by curly braces i.e. {} .
“let” and “const” are“block scoped”.

const: The scope of a variable defined with the keyword “const” is limited to the block defined by curly braces. However if a variable is defined with keyword const, it cannot be reassigned.
“const” cannot be re-assigned to a new value. However it CAN be mutated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Module Pattern in JavaScript

A

It is a commonly used Design Pattern which is used to wrap a set of variables and functions together in a single scope.

Variable “salary” is not exposed outside the “EmployeeDetails” function, making it unavailable for the “newEmployee” Object.
It can now be seen as a private variable that can be accessed only from the function. It is not available outside.

function EmployeeDetails() {
  var name: "Mayank";
  var age = 30;
  var designation = "Developer",
  var salary = 10000;
  var calculateBonus = function(amount) {
    salary = salary + amount;
  }
  return {
    name: name,
    age: age,
    designation: designation,
    calculateBonus: calculateBonus
  }
}

var newEmployee = EmployeeDetails()

var userName = newEmployee.calculateBonus(1000);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

feature inference

A

Feature inference checks whether a feature exists first before running another function that depends on the feature.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

feature detection

A

Feature detection is the pattern of running different code depending on whether a browser supports a certain block of code or not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

UA string

A

UA String is a browser-reported string that allows the network peers to identify the application type, operating system and other information about the request agent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a curry function?

A

Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. It returns a new function that expects the next argument inline.

Currying creates nesting functions according to the number of the arguments of the function. Each function receives an argument. If there is no argument there is no currying.
If function has multiple arguments, that’s partial application.

function volume(l) {
    return (w) => {
        return (h) => {
            return l * w * h
        }
    }
}
const aCylinder = volume(100)(20)(90) // 180000
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When to use currying?

A
  1. Write little code modules that can be reused and configured with ease:
function discount(discount) {
    return (price) => {
        return price * discount;
    }
}
const tenPercentDiscount = discount(0.1);
const twentyPercentDiscount = discount(0.2);
  1. Avoid frequently calling a function with the same argument:
function volume(h) {
    return (w) => {
        return (l) => {
            return l * w * h
        }
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

General Curry Function

A
function curry(fn, ...args) {
    return (..._arg) => {
        return fn(...args, ..._arg);
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between document load event and document DOMContentLoaded event?

A

The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

document’s load event is only fired after the DOM and all dependent resources and assets have loaded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the disadvantages of using Ajax?

A

Browser-compatibility and SEO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Ajax?

A

Asynchronous JavaScript and XML

web technology that allows applications to send data to and retrieve from a server without interfering with the behavior of the current page.

17
Q

Why is extending built-in JavaScript objects not a good idea?

A

Using a few libraries that both extend the Array.prototype by adding the same contains method, the implementations will overwrite each other and your code might break in unexpected ways.

18
Q

What are Promises?

A

Promises allows asynchronous methods return values like synchronous methods

A pending promise can either be fulfilled with a value, or rejected with a reason (error).

19
Q

What is “variable hoisting”?

A

Hoisting describes how variables declared with the var keyword will have their declaration “moved” up to the top of the current scope.

With const, just as with let, the variable is hoisted to the top of the block.

Only the declaration is hoisted, the assignment will stay where it is.

20
Q

How is Function.prototype.bind used?

A

The bind() method creates a new function that has the executing context (this) of the first parameter passed in.

It is used to change the executing scope of this function in cases where it is not provided.

21
Q

What is event bubbling?

A

When an event happens on a DOM element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors and all event listeners are triggered.

The most deeply nested element that caused the event is called a target element, accessible as event.target.

22
Q

Can event bubbling be stopped?

A

The method for it is event.stopPropagation().

Sometimes ir creates hidden pitfalls that later may become problems, e.g if we decide to track user clicks.

23
Q

What are the advantages of immutability?

A
  • Improves performance: eliminates planning for the object’s future changes.
  • Reduces memory use. It makes object references instead of cloning the whole object.
  • Provides thread-safety. Multiple threads can reference the same object without interfering with one other.
24
Q

What is “use strict”;?

A

use strict is a statement used to enable strict mode to entire scripts or individual functions.

25
Q

Advantages of strict mode

A
  • Eliminates some JavaScript silent errors by changing them to throw errors.
  • sometimes makes code run faster
  • throws errors, when relatively “unsafe” actions are taken
  • It disables features that are confusing or poorly thought out.
  • Strict mode makes it easier to write “secure” JavaScript.
26
Q

What’s the difference between .call and .apply?

A

Both are used to invoke functions and the first parameter will be used as the value of this within the function.

The main differences between bind() and call() is that the call() method:

  1. Accepts additional parameters as well
  2. Executes the function it was called upon right away.
  3. The call() method does not make a copy of the function it is being called on.

The .call function takes in comma-separated arguments as the next arguments while .apply takes in an array of arguments as the next argument.

27
Q

What is the difference between == and ===?

A

== is the abstract equality operator, will compare for equality after doing any necessary type conversions.

=== is the strict equality operator, will not do type conversion

28
Q

Event delegation

A

is a technique of handling events at a higher level in the DOM than where the element where they originated.

Event listeners are added to a parent element and will be triggered due to the event propagating up the DOM.

29
Q

event propagation

A

When a click event happens (or any other event that propagates):

  1. The event travels down from window, document, root element and through the ancestors of the target element (capture phase)
  2. The event occurs on the target (the target phase)
  3. Finally, the event bubbles up through the target’s ancestors until the root element, document and window (the bubble phase).
30
Q

What is a ternary expression?

A

A ternary expression is a special type of conditional that accepts three operands: the test condition, the “then” expression and the “else” expression.