Javascript Flashcards
(38 cards)
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