JS Object, This, & JSON Flashcards

1
Q

Can a property be deleted from an object?

A

There isn’t any method in an Object itself to delete its own properties (such as Map.prototype.delete()). To do so, one must use the delete operator.

(Map is preferable if deletion is necessary)

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

Object.assign()

A

Copies the values of all enumerable own properties from one or more source objects to a target object.

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

Object.create()

A

Creates a new object with the specified prototype object and properties.

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

Object.defineProperty()

Object.defineProperties()

A

Adds the named properties described by the given descriptors to an object.

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

Object.entries()

Object.fromEntries()

A

Returns an array containing all of the [key, value] pairs of a given object’s own enumerable string properties.

and the reverse, returns a new object from an iterable of [key, value] pairs.

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

Object.freeze()

Object.isFrozen()

A

Freezes an object. Other code cannot delete or change its properties.

Determines if an object was frozen.

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

Object.is()

A

Compares if two values are the same value. Equates all NaN values (which differs from both IsLooselyEqual used by == and IsStrictlyEqual used by ===).

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

Object.keys()

Object.values()

A

Returns an array containing the names of all of the given object’s own enumerable string properties.

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

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

Object.isSealed()

Object.seal()

A

Determines if an object is sealed.

Prevents other code from deleting properties of an object.

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

Object.preventExtensions()

Object.isExtensible()

A

Prevents any extensions of an object.

Determines if extending of an object is allowed.

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

Object.prototype.toLocaleString()

A

Calls toString().

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

Object.prototype.toString()

A

Returns a string representation of the object.

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

Object.prototype.valueOf()

A

Returns the primitive value of the specified object.

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

JSON

A

The JSON object contains methods for parsing JavaScript Object Notation (JSON) and converting values to JSON.

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

JavaScript and JSON differences

A

JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null. It is based upon JavaScript syntax but is distinct from it: some JavaScript is not JSON.

Objects and Arrays
for JSON: property names must be double-quoted strings; trailing commas are forbidden.

Numbers
Leading zeros are prohibited. A decimal point must be followed by at least one digit. NaN and Infinity are unsupported.

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

JSON.parse(text[, reviver])

A

Parse the string text as JSON, optionally transform the produced value and its properties, and return the value. Any violations of the JSON syntax, including those pertaining to the differences between JavaScript and JSON, cause a SyntaxError to be thrown.

17
Q

JSON.stringify(value[, replacer[, space]])

A

Return a JSON string corresponding to the specified value, optionally including only certain properties or replacing property values in a user-defined manner. By default, all instances of undefined are replaced with null, and other unsupported native data types are censored. The replacer option allows for specifying other behavior.

18
Q

JSON example

A
{
  "browsers": {
    "firefox": {
      "name": "Firefox",
      "pref_url": "about:config",
      "releases": {
        "1": {
          "release_date": "2004-11-09",
          "status": "retired",
          "engine": "Gecko",
          "engine_version": "1.7"
        }
      }
    }
  }
}
19
Q

JS Object Example

A

const car = {type:”Fiat”, model:”500”, color:”white”};

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue",
};
20
Q

Object Properties Shorthand

A
Function makeStatObj(array) {
    const Max = Math.max(…array);
    const Min = Math.min(…array);
    const sum = array.reduce((sum, value) => sum + value);
return { max, min, sum }; // object }
21
Q

How can you make computed properties for an object?

A

use a variable as a key name in an object literal property

const user = 'John'
const role = 'Admin'
const userRoles = {
    [user]: role      // John: "Admin"
}
22
Q

How can we create object methods?

A
const coolObject = {
  Name: "coolObject",
  speak() {
    console.log(`I am a cool object`);
  }
}

OR

const coolObject = {
  name: "coolObject",
  speak: function() {
    console.log(`I am a cool object`);
  }
}
23
Q

How would you use this in an object/class?

A
const coolObject = {
  first: 'cool'
  last: 'object'
  fullName() {
    return `${this.first} ${this.last};
  }
  sayHello() {
    const name = this.fullName();
    console.log(`Hi, I am ${name}!`);
  }
}
24
Q

if a function with this is reassigned in the global scope, what will ‘this’ refer to?

A

The window object

25
Q

If you use this in an arrow function (method) what will it refer to?

A

the global scope (window in the browser).
It is advised to not use arrow functions to define methods in objects/classes

instead use a method declaration to house an arrow function.
(if necessary when using global functions)

26
Q

Do arrow functions get their own ‘this’?

A

No, this will refer to the global object in an arrow function

27
Q

what will ‘this’ refer to if used in a setInterval() function?

A

The global object (window in browsers)

28
Q

how can you use a global function with this in an object/class?

A

with an arrow function, because it does not get it’s own scope.

const coolObject = {
    keyPhrase: 'I am so cool.',
repeatPhrase() {
     this.timerId = setInterval(() => { 
         console.log(this.keyPhrase);  //inherits scope from repeatPhrase()
     }, 5000)
}
stopPhrase() {
     clearInterval(this.timerId);
     console.log('cool object has stopped');
} }