JavaScript Flashcards
List ways to create an Object
- Object constructor :
var obj = new Object() - Object’s create method
var obj = Object.create(null) - Object literal syntax
var obj = {} - Function constructor
function Person(name) {}
var obj = new Person(“pat”) - Function constructor with prototype :
function Person() {}
Person.prototype.name = “Sudheer”;
var object = new Person(); - ES6 Class syntax
class Person {
constructor(name) {
this.name = name;
}
}
var object = new Person(“Sudheer”);
7. Singleton pattern
var object = new (function () {
this.name = “Sudheer”;
})();
What is prototype chaining
A mechanism in JS that allows objects to inherits from another object
เป็นกรไรของ JS ที่ให้ object สามารถ สืบทอดต่อจาก object ตัวอื่น
What is the difference between call(), apply() and bind()
Call()
invokes function with a given THIS value and arguments
Apply()
Similar to Call() just with an array
Bind()
Returns new function and allow you to pass any number of arguments
What is JSON and its common operations
JSON is a text-based format following JS object syntax.
Common op :
Parse : convert string to native object
stringify : convert native object to string
What is the purpose of slice and splice methods?
Slice :
Selects elements in a given array and return a new array with the selected element.
Splice:
Used either to add/ remove items to/from an array and then returns the removed item.
what’s the differences between Object and Map?
Object:
- key are strings and symbol and are unordered
- Have to manually calculate the size
- Can’t directly iterate over Object without getting its keys in some way and iterate over them
Map
- Keys can be any value including functions, object and primitives
- Keys in map is ordered
- Can easily get size of map with size()
- Iterable directly
- Perform better in scenarios liek frequent addition and removal of key pairs
What are arrow functions?
A shorter syntax for function expression and doesn’t have its own this
, arguments, super, or new.target.
What is first class functions ?
When functions in that language are treated like other variables (like passing to other function or returned by a function)
What is first order functions ?
Function that doesn’t accept another function as argument and doesn’t returns a function as value
what is higher order functions ?
Functions that accept another function as an argument or returns a function
What is unary functions?
A function that accepts exactly one argument.
Const unaryFunc = (a) => {}
what is currying functions ?
The process of taking a function with multiple arguments and turning it into a sequence of functions each with only one argument.
what is pure function ?
Function that the return value is determinded by its argument without any side effects (or any changes from outer code)
What are the difference between let, const and var?
let and const are block scoped
variable with let can be reassigned while const cannot
var is a global scoped variable.
How do you redeclare variables in switch block without and error
create a nested block inside a case clause and create a new block scoped lexical environment.
let counter = 1;
switch (x) {
case 0: {
let name;
break;
}
case 1: {
let name; // No SyntaxError for redeclaration.
break;
}
}
What is Temporal Dead Zone?
is a behavior in JavaScript that occurs when accessing a let or const variable before its declaration (within its scope) causes a ReferenceError. The time span when that happens, between the creation of a variable’s binding and its declaration, is called the temporal dead zone.
What is IIFFE
is a JavaScript function that runs as soon as it is defined
What is Hoisting ?
mechanism where variables, function declarations and classes are moved to the top of their scope before code execution