Objects, Functions, Classes - 25% Flashcards

1
Q

Create an obj

A

Object literal:
const obj = { a: “b” };

Object constructor:
const obj = new Object( )

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

delete operator

A

Use to delete a key/value pair from an obj:
delete myObj.key;

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

keys w/space

A

use quotation
ex: { “my key”: 2 }

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

existence of a property in an obj

A

Can use “in” operator to test for existence of a property:
“nonExist” in myObj // false

Reading a non-existent property returns undefined (not null):
myObj.nonExist === undefined

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

Obj Iteration

A

for (key in object) { }

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

Object property order

A

Properties that have keys that can be converted to Integers are sorted, the rest are in creation order

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

Object Inheritance

A

All objects inherit from at least 1 other obj
The object being inherited from is called the prototype,
and the inherited properties can be found in the prototype object of the constructor
You can add a property to all objects created through a certain constructor using the prototype property:
Car.prototype.color = ‘red’;

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

“this” for obj

A

Can use w/in a method to refer to the current object.
In a Class, cannot access “this” from w/in a nested fn, unless using arrow fn nested once (skips closest encompassing layer)

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

getters and setters

A

Within object initializers, they are prefixed with the keywords get or set.
When the getter is called, a fn is run and a value is returned.
When a setter is called, a parameter is accepted and a fn runs

const myObj = {
a: 7,
get b() {
return this.a + 1;
},
set c(x) {
this.a = x / 2;
},
};
console.log(myObj.a); // 7
console.log(myObj.b); // 8
myObj.c = 50; // Calls the set c(x) method
console.log(myObj.a); // 25

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

Comparing objs

A

Can’t be done with equality operator - 2 objs aren’t equal even if same keys/values.
For simple objs, can use JSON.stringify for comparison, but if key/vals aren’t in same order, it won’t work.

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

JS Class

A

A type of fn used to create mult objs w/the same properties and methods.
Also used to implement OOP concepts in JS, such as inheritance, encapsulation, and polymorphism.

It was introduced in (ES6) as a new syntax over the existing prototype-based inheritance.
They provide a more intuitive and clear syntax to create objects and deal with inheritance.

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

constructor

A

Creates a new object,
Binds ‘this’ to the new object,
Runs the code in the constructor,
Returns the new object.
Ex:
class Person {
constructor(name) {
this.name = name;
}
}
const gal = new Person(“Court”);
^ this calls the constructor fn

declaring it is optional, all will have a constructor fn built in.

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

Class Inheritance

A

Use “extends” keyword to declare a subclass, and inherit the properties and methods of the parent class.
Uses super( ) in the constructor method to pass the parent properties up, then can add its unique initialization.

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

Encapsulation

A

If a property or fn is declared using #, it is private and can only be reached from inside that Class.

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

Modules

A

Ways of splitting JS programs into separate units that can be imported when needed. Node.js has this ability and some JS libraries / frameworks use them.

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

Module usage

A

The <script type="module"> attribute is used to denote when a module is being pointed to.

Can export props and fns by adding to end of file:
export { name, fn1, fn2 };
or exporting each individually. Can’t use * w/export.

Use import statement - import { name, fn1, fn2 } from “./modules/square.js”;
The path - starting w/dot means starting at the current location.

Can use “as” in the import to change their reference keyword.

17
Q

Types of Scope

A

Global scope: The default scope for all code running in script mode.
Module scope: The scope for code running in module mode.
Function scope: The scope created with a function.
Block scope: The scope created with a pair of curly braces (a block) - this is for let and const, not var

18
Q

Execution flow

A

Top-down.
JS first checks for function and variable declarations and then executes the code. This concept is known as hoisting.

Ex - var
console.log(myVar); // This will output: undefined
var myVar = 5;
console.log(myVar); // This will output: 5

Ex - let (const is the same)
console.log(myLet); // ReferenceError: myLet is not defined
let myLet = 5;
console.log(myLet); // 5

19
Q

continue (in a loop)

A

The continue statement skips an iteration if a certain condition returns true.

20
Q

decorators

A

A wrapper fn that wraps an existing fn and extends its behavior Can be used to extend bx of objects without affecting other objects of the same class.
When it is a Class decorator, should receive only 1 arg.

21
Q

closure fn

A

A fn that has access to the parent scope, even after the parent function has closed. This means that a fn defined in the scope of another fn will continue to have access to variables in its parent fn and global scope, even after the parent fn has finished executing.

22
Q

privileged fn

A

A public fn that has access to private fns or variables. In other words, it is a method which is accessible in the public scope but has access to private data or methods.

Privileged methods are usually created by declaring them with the ‘this’ keyword inside the constructor.

23
Q

property descriptors

A

writable - boolean, can use to make readonly.

An obj that allows you to define or modify the attributes of existing properties on an obj. Attributes:

  1. Value: The value associated with the property. Can be any valid JavaScript value (number, object, function, etc).
  2. Writable: A Boolean indicating whether the value of the property can be changed.
  3. Enumerable: A Boolean indicating whether the property can be enumerated.
  4. Configurable: A Boolean indicating whether the property can be changed and deleted from the object.
  5. Get: A function which serves as a getter for the property, or undefined if there is no getter. The function’s return value will be used as the value of the property.
  6. Set: A function which serves as a setter for the property, or undefined if there is no setter. The function will receive as its only argument the new value being assigned to the property.

These descriptors can be accessed using methods like Object.getOwnPropertyDescriptor() and can be defined or modified using methods like Object.defineProperty() and Object.defineProperties().

24
Q

Object.entries

A

Returns an array containing all of the [key, value] pairs of a given object’s own enumerable string properties.
Object.fromEntries() - reverse of Object.entries:
Returns a new object from an iterable of [key, value] pairs.

25
Q

Object.values

A

Returns an array containing the values that correspond to all of a given object’s own enumerable string properties.

26
Q

Object.preventExtensions

A

Prevents obj from being extended.
Object.isExtensible gives boolean.

27
Q

Object.seal

A

Prevents other code from deleting properties of an object.
Object.isSealed gives boolean

28
Q

Object.freeze

A

Freezes an object. Other code cannot delete or change its properties.
Object.isFrozen gives boolean.

29
Q

Object.getOwn….

A

Object.getOwnPropertyDescriptor()
Returns a property descriptor for a named property on an object.

Object.getOwnPropertyDescriptors()
Returns an object containing all own property descriptors for an object.

Object.getOwnPropertyNames()
Returns an array containing the names of all of the given object’s own enumerable and non-enumerable properties.

Object.getOwnPropertySymbols()
Returns an array of all symbol properties found directly upon a given object.

30
Q

Object.assign

A

Copies the values of all enumerable own properties from one or more source objects to a target object.
ex:
Object.assign(target, obj)
- can have mult objs that will combine into the target obj

31
Q

Object.create

A

pass in a prototype obj to create a new one
Object.create(protoObj)

32
Q

higher-order fn

A

A fn that can take one or more fns as args, or can return a fn as its result. This is possible because in JS, fns are first-class citizens, meaning they can be assigned to variables, stored in data structures, passed as arguments to other functions, or returned as values from other functions. Higher-order functions are a key component of functional programming in JavaScript. Examples of higher-order functions include map(), filter(), and reduce().

33
Q

iterator v generator

A

Iterator:
An obj that provides a next() method which returns the next item in the sequence. This method returns an obj w/2 properties: done and value. The done property is a boolean that is true if the sequence is finished, and false otherwise. The value property is the current item in the sequence.

Generator:
A fn that can stop midway and then continue from where it left off. Generators simplify the task of writing iterators. They produce a sequence of results instead of a single value. Each call to the generator fn returns the next value in the sequence.

Comparison:
- Iterators allow you to customise the iteration logic whereas generators handle the iteration logic for you.
- Iterators require you to implement the next() method whereas generators automatically have a next() method.
- Generators use the yield keyword to yield the sequence of values, iterators do not have such a feature.
- Generators can also use the return statement to specify the final value of the sequence.
- Iterators can be infinite, generators cannot be infinite because they should have an end to the sequence.
- With a generator, you can use a loop to iterate through the values, whereas with an iterator, you have to manually call the next method until done is true.
- Generators can be easier to write and read when the logic to produce the sequence of values is complex.

34
Q

using .prototype to add a fn to a class

A

Changes to the prototype won’t affect existing instances of the object, only future instances. So if you add a new method to the prototype after creating an instance, that instance won’t have access to the new method.

35
Q

arguments obj of a fn

A
  • an array-like collection of vals
  • contains all the vals passed to the fn
  • only avail for non-arrow fns
  • the argument keyword can be used to access, for ex:
    argument[0] will be 1st arg passed.
36
Q

recursive fn

A

A fn that calls itself until it reaches a specified condition, known as the base case.
Useful in various scenarios, such as traversing tree-like data structures (like the DOM in web browsers or JSON objects), performing depth-first searches, solving complex mathematical calculations like factorials or Fibonacci sequences, and more. They can make code more readable and easier to understand, but they can also lead to high memory usage and stack overflow errors if not implemented carefully.

37
Q

static methods in a class

A

Static methods cannot be called through a class instance.

A static method in JS is a method that belongs to a class rather than an instance of the class. This means that you can call the method on the class itself, rather than on an object created from the class.

The main difference between a static method and a regular method (also known as instance method) is that the static method doesn’t know anything about the class state. It can’t modify the object state as it is not bound to any instance of the class.