JS Flashcards

1
Q

Getters

A

Getters are methods that get and return the internal properties of an object

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

how to use type of

A

typeof 37 === ‘number’;

Different types: number, undefined, object, boolean, string, symbol, function

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

Setters

A

methods which reassign values of existing properties within an object. Let’s see an example of a setter method:

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

How to turn an integer to a string?

A

.toString()

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

How to turn a string into an integer?

A

Number(x)

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

How to use a getter

A

from a ‘get’ variable in the object.

console.log(robot.getLevel);

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

How to use a setter

A

from a ‘set’ variable in the object

robot.getLevel = 100;

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

What is a state?

A

A program’s state is its current condition in regards to stored memory, variables, etc. It’s state can change over time as new input is added or data is manipulated in some way.

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

What is recursion?

A

Recursion is when a function calls upon itself until it reaches some sort of base case.

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

What is routing?

A

SPAs are based on a single document model. This means that web applications’ lifespan happens on a single html page, along with the transitions between the different views. But since links no longer imply the fetching and generation of a new document, how are those transitions modelled? they are achieved by using a router.
What is a Javascript Router?

A Javascript router is a key component in most frontend frameworks. It is the piece of software in charge to organize the states of the application, switching between different views. For example, the router will render the login screen initially, and when the login is successfull it will perform the transition to the user’s welcome screen.

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

What is abstraction?

A

Abstraction is a method for arranging complexity of systems. Abstraction contains complexity to create building blocks which are easier to expand upon.

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

encapsulation

A

Encapsulation is the process of restricting direct access to data or objects. Instead, methods are created to access this data or objects.

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

polymorphism

A

The ability to create a variable function or object that has more than one form. With polymorphism the program responds correctly depending on input type.

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

How to get object keys

A

Object.keys(obj1)

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

How to get an array of key value pairs of an object

A

Object.entries(obj1)

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

How to add more key value pairs to an existing object

A

const newRobot = Object.assign(robot, {laserBlaster: true, voiceRecognition: true});

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

The “for…in” loop

A
for (key in object) {
  // executes the body for each key among object properties
}

For instance, let’s output all properties of user:

let user = {
  name: "John",
  age: 30,
  isAdmin: true
};
for (let key in user) {
  // keys
  alert( key );  // name, age, isAdmin
  // values for the keys
  alert( user[key] ); // John, 30, true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Classes

A

Classes are a tool that developers use to quickly produce similar objects. Serves as a template for creating new objects.

19
Q

Difference between class and object syntax?

A

there is one important method that sets them apart. It’s called the constructor method. JavaScript calls the constructor() method every time it creates a new instance of a class.

class Dog {
  constructor(name) {
    this.name = name;
    this.behavior = 0;
  }
}
20
Q

create new object from class

A

const halley = new Dog(‘Halley’);

21
Q

What is inheritance

A

With inheritance, you can create a parent class (also known as a superclass) with properties and methods that multiple child classes (also known as subclasses) share. The child classes inherit the properties and methods from their parent class.

22
Q

What is a constructor?

A

The constructor method is a special method for creating and initializing an object created within a class.

23
Q

How do you make a class?

A

class Cat { }

24
Q

How do you make a constructor in a class?

A
class Cat {
  constructor(name, usesLitter) {
    this._name = name;
    this._usesLitter = usesLitter;
    this._behavior = 0;
  }
}
25
Q

Why use inheritance?

A

Because children can inherent properties from their parents it is a way of reducing code. One benefit is that when you need to change a method or property that multiple classes share, you can change the parent class, instead of each subclass.

26
Q

What are methods?

A

Methods are properties of an object that hold function values

27
Q

How to create a child class?

A
class Cat extends Animal {
  constructor(name, usesLitter) {
    super(name);
    this._usesLitter = usesLitter;
  }
}
28
Q

When making a child class where does super go?

A

In a constructor(), you must always call the super method before you can use the this keyword — if you do not, JavaScript will throw a reference error. To avoid reference errors, it is best practice to call super on the first line of subclass constructors.

29
Q

What is a static method?

A

methods that aren’t available in individual instances, but that you can call directly from the class.

Take the Date class, for example — you can both create Date instances to represent whatever date you want, and call static methods, like Date.now() which returns the current date, directly from the class. The .now() method is static, so you can call it directly from the class, but not from an instance of the class.

30
Q

What is the static method syntax?

A
static generateName() {
    const names = ['Angel', 'Spike', 'Buffy', 'Willow', 'Tara'];
    const randomNumber = Math.floor(Math.random()*5);
    return names[randomNumber];
  }
31
Q

return a random number between 0 and 10000

A

return Math.floor(Math.random() * 10001;

32
Q

What is package.json?

A

A package.json file contains information about the current JavaScript project. Some of this information includes:

Metadata — This includes a project title, description, authors, and more.
A list of node packages required for the project — If another developer wants to run your project, npm looks inside package.json and downloads the packages in this list.
Key-value pairs for command line scripts — You can use npm to run these shorthand scripts to perform some process. In a later exercise, we will add a script that runs Babel and transpiles ES6 to ES5.

If you have Node installed on your computer, you can create a package.json file by typing npm init into the terminal.

33
Q

What does the -D do? In npm install babel-cli -D

A

The -D flag instructs npm to add each package to a property called devDependencies in package.json. Once the project’s dependencies are listed in devDependencies, other developers can run your project without installing each package separately. Instead, they can simply run

34
Q

What does npm install do?

A

it instructs npm to look inside package.json and download all of the packages listed in devDependencies.

35
Q

How to create a package.json file?

A

From the terminal, run npm init and type the above values

36
Q

What are JavaScript modules?

A

JavaScript modules are reusable pieces of code that can be exported from one program and imported for use in another program.

37
Q

What are the pros of modules?

A

Modules are particularly useful for a number of reasons. By separating code with similar logic into files called modules, we can:

find, fix, and debug code more easily;
reuse and recycle defined logic in different parts of our application;
keep information private and protected from other modules;
and, importantly, prevent pollution of the global namespace and potential naming collisions, by cautiously selecting variables and behavior we load into a program.
38
Q

How do you export a module?

A

module.exports = Menu;

39
Q

How to import a module in ES5?

A

const Menu = require(‘./menu.js’);

40
Q

What is an anonymous function?

A

Anonymous functions are functions that are dynamically declared at runtime. They’re called anonymous functions because they aren’t given a name in the same way as normal functions.

Anonymous functions are declared using the function operator instead of the function declaration. You can use the function operator to create a new function wherever it’s valid to put an expression. For example you could declare a new function as a parameter to a function call or to assign a property of another object.

Here’s a typical example of a named function:

    function flyToTheMoon()
    {
      alert("Zoom! Zoom! Zoom!");
    }
    flyToTheMoon();

Here’s the same example created as an anonymous function:

    var flyToTheMoon = function()
    {
      alert("Zoom! Zoom! Zoom!");
    }
    flyToTheMoon();
41
Q

In ES6 what are the two new ways for exporting modules?

A

default export and named exports.

We’ll begin with the first syntax, default export. The default export syntax works similarly to the module.exports syntax, allowing us to export one module per file.

Let’s look at an example in menu.js.

let Menu = {};

export default Menu;

named exports allow us to export data through the use of variables.

Let’s see how this works. In menu.js we would be sure to give each piece of data a distinct variable name:

let specialty = '';
function isVegetarian() {
}; 
function isLowSodium() {
}; 

export { specialty, isVegetarian };

42
Q

How do you import in ES6?

A

import Menu from ‘./menu’;

43
Q

How can you change the name of modules as you export or import them?

A

Named exports also conveniently offer a way to change the name of variables when we export or import them. We can do this with the as keyword.

Let’s see how this works. In our menu.js example

let specialty = '';
let isVegetarian = function() {
}; 
let isLowSodium = function() {
}; 

export { specialty as chefsSpecial, isVegetarian as isVeg, isLowSodium };

44
Q

How to throw an error

A

throw new Error(‘Request failed!’);