Javascript

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Given the following code, what does 'this' refer to? const box = document.querySelector('.box'); box.addEventListener('click', function() { console.log(this) })

'this' refers to the object the function is attached to because the context of 'this' is bound to it's new local scope with pre ES6 functions.

Given the following code, what does 'this' refer to? const box = document.querySelector('.box'); box.addEventListener('click', () => { console.log(this) })

'this' refers to the window object. The reason being because with arrow functions 'this' is not rebound to a new scope.

What is the output? var num = 4; function outer() { var num = 2; function inner() { num++; var num = 3; console.log(num) } inner(); } outer();

3

function declaration vs function expression

A function declaration is a named function wheras a function expression is not named. It is a function stored in a variable. A function that is defined using a function declaration is hoisted where a function expression is not because it is treated like a variable. If you want to pass a function into another function you must use a function expression. function funcA(name) { console.log(name) } const funcB = function(name) { console.log(name) }

Immediately Invoked Function Expression (IIFE)

A function that is run as soon as it is defined. It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts: The first is the anonymous function with lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope. The second part creates the immediately invoked function expression () through which the JavaScript engine will directly interpret the function.

What are the primitive types in javascript?

A primitive is not an object and has no methods of its own. All primitives are immutable. These six types are considered to be primitives: string, boolean, number, null, undefined, symbol

What is a promise?

A promise is a function which immediately returns a value is either resolved or rejected. Once that value is returned, you use a callback function to deal with that value in some way. let promiseDemo = new Promise((resolve, reject) => { resolve(fetch(url)) })

What operations should be able to be performed on Data Structures?

Access and read values Search for an arbitrary value Insert values at any point into the structure Delete values in the structure

Difference between '==' and '==='.

Both '==' and '===' are comparison operators. '==' evaluates two values as being roughly equal while '===' evaluates two values as being strictly equal. '==' is considered with comparing two values only and not the data types whereas '===' compares both. An example of values that are roughly equal are null and undefined, 1 and '1'. When doing a rough comparison, the program tries to convert types to equal each other and follows a hierarchy of values that it converts to. For example if one value is a string and the other is a number, the program will convert the number to a string and see if they are equal.

True/False the properties of a const object are immutable

False. The properties of an object declared using const can be updated. You just declare that variable again.

temporal dead zone

If a variable declared by the var keyword is called before it is declared, it will return undefined, but not throw an error. Let and Const will throw an error if you do this.

Describe the array data structure as applied to JavaScript

JavaScript does not have an explicit array data type and arrays are represented by the Array object which is a list like object. Array is an Object type with special constructor and accessor methods. Its prototype has methods to perform traversal and mutation operations. JavaScript arrays are heterogeneous, meaning the types of elements are not fixed and neither is the size. Arrays in JavaScript are not contiguous data structures and the data stored in the array can be located in a non-contiguous location. There are several ways to create an array:

What is a call stack?

Javascript is a single threaded single concurrent language, meaning it can handle one task at a time or a piece of code at a time. It has a single CALL STACK which along with other parts like heap, queue constitutes the Javascript Concurrency Model. The call stack is a data structure which records the function calls, basically where in the program we are. If we call a function to execute , we push something on to the stack, and when we return from a function, we pop off the top of the stack.

The only JavaScript value that is not equal to itself.

NaN (Not a Number)

unary operator

Operators that use two values are called binary operators. Not all operators are symbols. Some are written as words. One example is the type of operator, which produces a string value naming the type of the value you give it. Typeof takes only one operator, therefor is unary. The minus operator can be used as a binary operator as a unary operator because it can both perform an operation on two numbers or flip a value to become negative.

How can you store an arrow function in a variable

const sayMyName = (name) => { alert(name); };

Create A Binary Search Function Through Iteration, and One Through Recursion

function binarySearch(array, target) { let left = 0; let right = array.length - 1; while (left <= right) { const mid = left + Math.floor((right - left) / 2) if (array[mid] === target) { return mid; } if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; }

Create a secret variable.

function privateVariable() { let private = "secret"; return function() { return private; } } let reveal = privateVariable(); console.log(reveal())

3 benefits of arrow functions

more concise, implicit return, do not rebind the value of "this"

What's a difference between var, let, and const

var is function scoped, whereas let and const are block scoped.

undefined vs. null

null and undefined both represent empty values. When you first declare a variable and don't immediately assign it a value, it is automatically undefined as a placeholder until it is assigned a value. typeof null would return an object whereas typeof undefined will return undefined.

// What is the output? var hero = { _name: 'John Doe', getSecretIdentity: function() { return this._name; } } var stolenSecretIdentity = hero.getSecretIdentity; console.log(stolenSecretIdentity()); console.log(hero.getSecretIdentity());

undefined "John Doe" stolenSecretIdentity() returns as undefined because 'this' in the getSecretIdentity method is no longer bound to the object. You would have to bind the method. var stolenSecretIdentity = hero.getSecretIdentity.bind(hero);

What's a difference between let and var?

Var can be overwritten if the same variable name is declared twice. You can update let, but you cannot declare it twice. 'var' is hoisted above the line of code it is declared at. You might not be able to access its value, but the program knows of its existence. 'let' is considered undefined above the line of code it is declared at.

Prototypal Inheritance

When an object is created from an existing object, the new object will receive objects from its predecessor. It won't actually have those properties itself, but will referene them from the parent object.

type coercion

When an operator is applied to the "wrong" type of value, JavaScript will quietly convert that value to the type it needs using a set of rules that aren't always what you would want or expect

How can you switch the value of the variables below using es6? value1 = 'foo' value2 = 'bar'

[value1, value2] = [value2, value1]

// What is the output? console.log(typeof typeof 1) Why?

String. typeof(1) would output 'number'. So, when you do typeof typeof 1 what you are really doing is asking the program to return typeof 'number'. Think of it in terms of the order of operations. The problem can be rewritten as console.log( typeof (typeof 1)). The inner typeof is returned first and it returns 'number' which is a string so the outer call to type of returns string.

Object.freeze()

The Object.freeze() method freezes an object: that is, prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed. In essence the object is made effectively immutable. The method returns the object being frozen.

Algorithm

The set of steps a program takes to finish a task. An algorithm should contain a clearly defined problem statement, input, and output. The steps in the algorithm need to be in a very specific order. The steps also need to be distinct. The algorithm should produce a result. The algorithm should complete in a finite amount of time.

Ternary operator

The ternary operator, formally known as the conditional operator, takes three operands. The first is a condition and the other two are values that will be returned conditionally based on the value of the first operand.

Big O

Theoretical definition of the complexity of an algorithm as a function of the size. An abstracted function that describes the amount of computer time or memory space required by an algorithm, as a function of problem size. For problems larger than a certain size, the actual time or space required will be less than the Big O multiplied by some constant. You'll also see Big O referred to as the upper bounds of the algorithm.

True/False: Arrow functions are always anonymous functions

True


Set pelajaran terkait

Principles of Economics 2e Chapters 1-8 Test Bank

View Set

Introductory Sociology 1101 - Lesson 8 Quiz (100%)

View Set

AP HUMAN GEOGRAPHY POPULATION UNIT TEST

View Set

Irritable Bowel Syndrome buttaro ch. 139

View Set

PSYC 3200 Exam 2 Questions (ch. 5, 6, 10)

View Set

Listening Guide Quiz 4: Farmer: Fair Phyllis

View Set

N101 final - Professionalism and Accountability

View Set

Interaction Design - Beyond human-computer Interaction

View Set