Javascript Flashcards
what is a dynamic language?
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
what is dynamic variable typing?
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
in javascript, whats the difference between “let” and “var”?
let: gives block scope (only accessible in block
var: global variable scope
what is “use strict” for?
adds more restrictions to get more error messages like undeclared variables
how do you get the type of variable stored in x?
console.log(typeof x);
show the 2 ways to implement a for loop
for (let i = 1; i < arr.length; i++) {
console.log(array[i]);
}
for (let num of array) {
console.log(num);}
how to declare a function with 3 parameters, and to return those 3 parameters.
funciton myFunc (p1, p2, p3) {
return p1 + p2 + p3
}
how do you access the parameters of a function?
arguments
what happens if there are less/more parameters in a functions call?
less: the rest are given a value of undefined
more: no names, but inside arguments array
show me the 2 ways to initialize an array:
let arr = [ 1, “hi”, true]
let myArr = new Array(10);
are primitive types in java immutable?
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
are all objects pointers? show me an example
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 to define class and constructor in js?
class MyClass {
constructor() {}
}
how to make instance variable “id”? how to declare in constructor?
class MyClass {
#id;
constructor(id){ this.#id = id;}
}
how to create a new instance of a class in js?
let mc = new MyClass(1234);
how to have multiple constructors?
constructor() {
if (arguments.length == 0) {
// do thing
}
else if (arguments.length == 1) {
// do thing
}
}
how to declare a method? how to make it private?
method() { // dothing
}
can make private by including # in front of method
how to make getter and setter function for an id? and how to call it?
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
how to declare static variable like a counter, and increment every time constructor is called?
class mc {
#id;
static #counter = 1;
constructor() {
this.#id = mc.counter++;
}
- class method must be accessed through class name
how to get a class constant?
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);
whats an alt way to create an object?
key-value dictionary:
instance variables are separated by commas:
aChair = { material: “wood”, weight: 5.5}
functions:
print: function() { // do stuff}
how to add a new method to an unnamed object, like, get weight in pounds?
aChair.getWeightInLbs = function() {
return this.weight * 2.2}
how to update a class in java script?
use .prototype to add fields and methods:
MyClass.prototype.name = “NoName”;
MyClass.prototype.getName = function() {return this.name;};
how to do deep and shallow object copies in javascript?
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
how to copy method in javascript?
let mcCopyProto = Object.assign(Object.getPrototypeOf(mc) mc);
** does not work if private fields are used
for a class A, how to export it to use in a different class?
module.exports = A;
let A = require(“./A.js”);
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”
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
how to assign superclass in runtime? say, make sub a subclass of superc
sub.prototype.__proto__ = new superc();
how to inherit a method from subclass that doesnt exist in superclass?
let mc = new superc();
superc.prototype.__proto__ = new sub();
superc.method(); // inherited!
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
how to override in js?
same as java, just declare smae method in subclass (params dont matter)
how to shadow in js?
only with private fields for vairables, but cannot shadow for methods (there is no static type)
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)
how to check if a method exists in an object?
if (typeof class.rpint == “function”) class.print();
how does instanceof operator work?
similar to java, but since we dont need to cast variables its pretty useless.
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”);
}
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);