javascript Flashcards

1
Q

how node treat module files

A

node never process the .js file directly, it wraps the module as a function and pass it to the chrome v8 engine

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

what’s the global object in node.js

A

global

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

what’s the global object in chrome

A

window; document

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

naming convention for class

A

ClassName

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

same folder path in require; parent folder path in require statement

A

same folder ./xxx(.js) parent folder ../xxx(.js)

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

how to load a module

A

require(‘’)

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

EMACscript 6 fancy string function is called?

A

Template String

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

the node.js built in packages

A

path os fs http events ( class: EventEmitter; methods: emit & on )

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

Since events package provides an EventEmitter class (not an instance), what needs to be paid attention when working on the events?

A

the actual registering event and emit event action need to be based

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

what makes the function become the special constructor function inside a class defination

A

give the function name as ‘constructor’, then it will work with the new key word

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

what the class sytax sugar does

A
  • generating the function that 1) has the name of the class given and 2) coding block as the constructor method - attach all other attributes to the .prototype attribute of the function with class name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

is most of the javascript keyword little cap

A

yes (new, function, class, cnst…)

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

how to inheriant from another class when defining an class

A

using ‘extends’ keyword

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

singular module returning object

A

module.exports =

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

is module global?

A

it is actual a local variable that node.js passed into the wrapped function of the module

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

how to see the wrapped function of the module

A

set a syntax error in the very 1st line of a module like ‘var =;’ and then require it somewhere else

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

how to set async spin over certain time interval

A
  1. return a new Promise 2. Inside the promise, define another async function 3. Inside the function, if goal reached, then resolved; if not, use setTimeout to sell the call again. (*** The async function is within the ‘new Promise’, so whenever it resolved, the same promise instance resolved) 4. Call the async function manuallyafter defination
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

how to set parameter’s default value

A

constructor ({ keyId, secretKey, paper = true, bucketPct = 0.25 }) { … }

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

var scope situation

A

var only socpe to either functional scope or global scope (window or global object)

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

let and const sope

A

they scope to the block scope (curly brackets)

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

what is Hoisting in JS

A

variable can be used before the definition. (hoisting means moving all declarations to the top of the current scope

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

what is a primitive type in JS

A

it is not an object and has no methods

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

primitive types are immutable, how to approve it

A

you can not use an string method to alter e.g. toUpperCase the data itself (on the other side, the array method e.g. push can alter the array); the string method can then only manipulate the return value

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

what is Symbol in JS

A

Symbols are often used to identify object properties.Often to avoid name clashing between properties, since no symbol is equal to another; No two symbols are the same ‘===’

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

null vs undefined

A

null is an object; undefined is a type null can be assigned to variable; undefined appears when nothing assigned to variable

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

Map vs Object

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

JS 3 data structues

A
  • Array, list of object
  • Map, key-value pair, simplified the historicially used Object
  • Set, unquie value of Array list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Date in JS

A
  • number of milisecond since 1970
  • new Date() - create a new Date object
  • Date.now()
  • Date.parse() - parse string date representation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Date - To get Date, Month and Year or Time

A

let [month, date, year] = new Date().toLocaleDateString(“en-US”).split(“/”) let [hour, minute, second] = new Date().toLocaleTimeString(“en-US”).split(/:| /)

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

Template String

A

string text ${expression} string text

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

Implicity Conversion

A

Implicit Coercion: Type conversion is done implicitly by JavaScript.

  • 12 + “” //Output is “12” as number 12 is converted to string “12”
  • “15” * 2 //Output is 30 as string 15 is converted to number 15
  • “15” - “11” //Output is 4 as both the strings are converted to number
  • undefined + 6 //Output is NaN as undefined could not be converted to number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Explicity Conversion

A
  • Number(“25”) //Output is 25 as “25” string is converted to number 25
  • String(25) //Output is “25” as 25 is converted to string “25”
  • Boolean(25) //Output is true
    *
34
Q

Comparison Operations in JS

A
  • loose compare: == (only compare value)
  • strict compare: === (compare value and type)
  • same-level compare: object.is()
35
Q

spread operator vs. rest operator

‘…’

A
  • Spread Syntax: […iterableObj, ‘4’, ‘five’, 6]
  • Rest Syntax: function f(a, b, …theArgs) { // … }
36
Q

how to get array length

A

.length

37
Q

using for loop through an array

A
38
Q

using ‘for … of ..’ to loop through the array

A
39
Q

using ‘for…in…’ to loop through object

A
40
Q

‘while’ loop

A
41
Q

‘while’ loop with ‘break’

A
42
Q

‘while’ loop with ‘continue’

A
43
Q

loop using ‘do.. while…’

A
44
Q

what’s the difference b/w ‘do..while..’ and ‘while’

A

do…while… always run for the first time

45
Q

‘for.Each…’ to loop through array

A
46
Q

why it is better to always come to forEach when looping through array

A

it is more data object driven

47
Q

Scope for ‘this’

A
  1. if ‘this’ defined inside a method, it always point to the object (not the method itself, but the object it belongs to);
  2. if ‘this’ defined inside a function, it point to the window/global object
    3.
48
Q

what is the secon parameter of forEach function

A

thisArgs, value to be used as this inside the first param - the call back function

49
Q

what is setter and getter in js

A
  • let the method can be accessed like a true property from outside the object
50
Q

does spread syntax has to be at last?

A

no, only rest syntax always has to be in the last position; spread syntax can be anywhere in side the parameter passing parenthesis

51
Q

Destructuring Assignment Syntax

A
52
Q

when the curly bracket can be ignore

A

when usiing ‘if’, ‘for’…

53
Q

constructor function vs. class

A
  • constructor function is a regular function to be invoked with the ‘new’ keyword
  • class has the ‘constructor’ method; also to be instansized with ‘new’ keyword
54
Q

thumb rule for determin the ‘this’ context:

which one is correct

  1. this is determined based upon where the function is called
  2. this is determined based upon where the function is defined
A

1 is the correct answer

55
Q

what’s the difference of using .bind() vs. .call() / .apply()

A

using .bind() to invoke a function create a persistent binding of this context that always connect to the function. so we can not eyeball it when just reading the code and always need to be searched the binding.

This probably should not be recommended

However, the .bind() can be used to fix the callback method missing this context issue

56
Q

5 binding types tips

  1. implicit binding
  2. Explicit binding
  3. new Binding
  4. Default Binding
  5. Callback Binding
A
  1. implicit binding - object.func() - bind to whatever left to the dot
  2. Explicit binding - .call(); .apply(); .bind(); forEach()…
  3. new Binding - new func()
  4. Default Binding - func() - bind to global object
  5. Callback Binding - loosing the implicit binding
57
Q

how to pass parameters to callback function when using setTimeOut or setTimeInterval

A

use the 3rd parameter:

setTimeOut( callBackFunc, , args )

58
Q

iterator signature method and what’s its returning value

A
  • .next()
  • { value: xxx, done: true/false }
59
Q

generator function syntax

A

function* xxx () {

yield

}

60
Q

inside generator function, how to delegate to another deeper level generator

A

yield* generatorFunc()

The yield* expression iterates over the operand and yields each value returned by it.

61
Q

yield as expression

A

achieving the goal for 2 way data exchange

62
Q
A
63
Q

Accessing the await-ed value inline (JS)?

A

async

64
Q

how to inline invoke (async) function

using grouping operator ( )

A

The grouping operator ( ) controls the precedence of evaluation in expressions.

65
Q

npm package.json vs. package-lock.json

A

https://stackoverflow.com/questions/45052520/do-i-need-both-package-lock-json-and-package-json

66
Q

node.js working with json file

A

package fs

function JSON.parse()

67
Q

deconstructing Assignment

A

[a, b, …rest] = [10, 20, 30, 40, 50];

const x = [1, 2, 3, 4, 5]; const [y, z] = x;

68
Q

what happens to the created object if adding attributes to the construction function’s .prototype object

A

The attribute will NOT be added directly to the created object, but will be attached to its __proto__ chain.

69
Q

node js package for database access and management

A

sequelize

70
Q

what is following statement:

$(function () {

// do stuff

});

A
72
Q

what is the term for Node js implementing the async behaviori

A

Non-blocking I/O

73
Q

which module helps fs package implement the sync file reading function - fs.readFileSync()

74
Q

when declaring class, how to define a method attribute

A

the function keyword can be ignored,

the same applies to async function - only keep async following by the function name is fine

75
Q

When passing callback function to setTimeOut, what if the target callback function is a method of an instance

A

Do not pass the function directly as when the callback function invoked, the scope changes.

put the callback function with ‘this.’ inside another wrapper function and pass the wrapper function as the parameter to setTimeOut.

76
Q

use destructuring in JavaScript function arguments

A

yes, this is a bit odd but it makes sense. And the deconstructed arguments can be used directly inside the function (even though it looks like a attribute inside a parameter object but it doesn’t need a top level object to be referred to)

77
Q

using switch…case… statement:

  1. what is the other option keyword
  2. must using break
  3. Special signal to use
A
  1. default
  2. Yes
  3. :
78
Q

does await works in .forEach? if not, what is the walk around

A

No, try to use for… loop

79
Q

Given the non-blocking I/O design in JS,

what are the issue presents by this architecture?

What are the values brought by Callback Function, Promises, Async Function in different level.

A
  • how to resume the process once the I/O finished - Callback
  • having a clear status of the I/O process - Promise’s status
  • In case where a blocking I/O is desired - use promise.then() or await
  • Disadvange of using callback or promise.then() - callback hell and promise hell
80
Q

why in node.js, we should always use ‘module.exports = ‘ rather than ‘ exports = ‘ when exporting single object

A

Because assign value to ‘exports’ directly will change the referenced object it is pointing to but the node js will only export to the object point by module.exports. (They were originally pointing to the same object)

https://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-node-js/26451885#26451885

81
Q

what’s the differece b/w Reset Parameters and The argument object

A
  • The arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly;
  • The arguments object has additional functionality specific to itself (like the callee property).
  • The …restParam bundles all the extra parameters into a single array, therefore it does not contain any named argument defined before the …restParam. Whereas the arguments object contains all of the parameters – including all of the stuff in the …restParam – unbundled.