Javascript Flashcards

(38 cards)

1
Q

what is a dynamic language?

A

a language where all/almost all things are done dynamically (features are done at run time), compile time is static
- heap based objects are allocated at runtime

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

what is dynamic variable typing?

A

where a variable’s type is whatever it currently contains, so there’s no type declaration (for example:
let x = 6 (number)
x = “hi” (now a string)

  • can be tricky because easy way to get errors by expecting one thing and getting another
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

in javascript, whats the difference between “let” and “var”?

A

let: gives block scope (only accessible in block

var: global variable scope

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

what is “use strict” for?

A

adds more restrictions to get more error messages like undeclared variables

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

how do you get the type of variable stored in x?

A

console.log(typeof x);

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

show the 2 ways to implement a for loop

A

for (let i = 1; i < arr.length; i++) {
console.log(array[i]);
}

for (let num of array) {
console.log(num);}

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

how to declare a function with 3 parameters, and to return those 3 parameters.

A

funciton myFunc (p1, p2, p3) {
return p1 + p2 + p3
}

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

how do you access the parameters of a function?

A

arguments

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

what happens if there are less/more parameters in a functions call?

A

less: the rest are given a value of undefined

more: no names, but inside arguments array

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

show me the 2 ways to initialize an array:

A

let arr = [ 1, “hi”, true]

let myArr = new Array(10);

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

are primitive types in java immutable?

A

yes, if variable’s value is assigned to another variable, a copy will be made:

let s1 = “abc”
let s2 = s1; (copy)
s2 = “xyz” (s1 is unchanged)

s1 = abc
s2 = xyz

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

are all objects pointers? show me an example

A

yes, all objects are dealt with via pointers, assignment operator makes a shallow copy:
let a1 = [“a”, “b”]
let a2 = a1;
a2[0] = “pizza”

a1 = pizza ,hello
a2 = pizza, hello

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

how to define class and constructor in js?

A

class MyClass {
constructor() {}
}

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

how to make instance variable “id”? how to declare in constructor?

A

class MyClass {
#id;
constructor(id){ this.#id = id;}
}

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

how to create a new instance of a class in js?

A

let mc = new MyClass(1234);

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

how to have multiple constructors?

A

constructor() {
if (arguments.length == 0) {
// do thing
}
else if (arguments.length == 1) {
// do thing
}
}

17
Q

how to declare a method? how to make it private?

A

method() { // dothing
}

can make private by including # in front of method

18
Q

how to make getter and setter function for an id? and how to call it?

A

set id (newId) {
this.#id = newId;
}

get id() {
return this.#id;
}

call:
let mc = new class(1234);
mc.id = 7500; // uses setter
console.log(mc.id); // using getter

19
Q

how to declare static variable like a counter, and increment every time constructor is called?

A

class mc {
#id;
static #counter = 1;

constructor() {
this.#id = mc.counter++;
}

  • class method must be accessed through class name
20
Q

how to get a class constant?

A

need to simulate with a getter static method that returns the value:

static get HOURS_PER_DAY() {
return 24;
}

access with:
console.log(MyClass.HOURS_PER_DAY);

21
Q

whats an alt way to create an object?

A

key-value dictionary:
instance variables are separated by commas:
aChair = { material: “wood”, weight: 5.5}

functions:
print: function() { // do stuff}

22
Q

how to add a new method to an unnamed object, like, get weight in pounds?

A

aChair.getWeightInLbs = function() {
return this.weight * 2.2}

23
Q

how to update a class in java script?

A

use .prototype to add fields and methods:
MyClass.prototype.name = “NoName”;
MyClass.prototype.getName = function() {return this.name;};

24
Q

how to do deep and shallow object copies in javascript?

A

let b = a:
makes them pointer to same memory, changing one changes both, a == b is true

deep copy:
let copy = new Object();
Object.assign(copy, a);

** note that object assign only copies fields and not methods

25
how to copy method in javascript?
let mcCopyProto = Object.assign(Object.getPrototypeOf(mc) mc); ** does not work if private fields are used
26
for a class A, how to export it to use in a different class?
module.exports = A; let A = require("./A.js");
27
does javascript have multiple inheritance? how do constructors work?
no multiple inheritance, also if no constructor in superclass, then default constructor automatically calls the super constructor if subclass has a constructor, the superconstructor must be called explicitly before any statement with "this"
28
what is inherited in javascript?
- instance variables are not inherited - methods are inherited - cannot override a super class' variable if its private in the parent class
29
how to assign superclass in runtime? say, make sub a subclass of superc
sub.prototype.__proto__ = new superc();
30
how to inherit a method from subclass that doesnt exist in superclass?
let mc = new superc(); superc.prototype.__proto__ = new sub(); superc.method(); // inherited!
31
how to use default parameters in a method's call?
method(message = "hello", number = 0){/stuff} - message will be hello if no paramter used, number will be 0 if less than 2 params
32
how to override in js?
same as java, just declare smae method in subclass (params dont matter)
33
how to shadow in js?
only with private fields for vairables, but cannot shadow for methods (there is no static type)
34
what is duck typing?
as long as an object possess the required method, we dont care what it is (if 2 different objects have a print(), then there will be no errors in calling it)
35
how to check if a method exists in an object?
if (typeof class.rpint == "function") class.print();
36
how does instanceof operator work?
similar to java, but since we dont need to cast variables its pretty useless.
37
how to make abstract class / method?
simulate class: class abs { constructor { if (this.constructor.name === "abs"); throw new Error("cant be"); - can also add methods in abstract class that will be inherited simulate method: method() { throw new error("method is missing"); }
38
what are mixins?
way to include outside code in a class w/o using inheritance: let myMixin = {method1(params) {body}}; to add the mixin, use: Object.assign(MyClass.prototype, myMixin);