Objects Flashcards

1
Q

3 ways to create a blank object

A

{}
new Object
Object.create(null)

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

Count number of enumerable properties an object has

A

Object.keys( obj ).length

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

Test if object contains property/method, whether directly or in the prototype chain.

A

“field_name” in obj

Do not use obj.field_name, as falsey values will evaluate to false. in also has the added benefit of the properties not being evaluated, which could be negative on performance.

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

Test if object contains property/method directly (owns).

A

obj.hasOwnProperty(“field_name”);

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

How to remove a property/method from an object?

A

delete obj.field_name //=> true|false

(properties with “configurable” set to false cannot be removed, so false would be returned. false is also returned if the property doesn’t exist)

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

Dereferencing an object?

A

Dereferencing an object tells JS that you no longer need the object so the built-in garbage collector can free up the memory being used by that object. This happens automatically in JS, but you need to dereference the object first; the best way to do that is set the object pointer’s variable to null:

var o = {};
// do something
o = null;  // if there aren't any other pointers stored to a variable, JS will garbage collect it :)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

3 ways to define a property (data-property)

A

(property fields must always be strings; if not observed, it is converted into a string)

o["prop"] = 1;
o.prop = 1;    // if the field name *could* be a valid variable name
// ECMAScript 5 way
// (3rd argument is called "Property Descriptor")
Object.defineProperty(o, "prop", {
    value: 1,
    // if a value can be reassigned
    writable: true,
    enumerable: true,
    // if prop can be deleted
    // and if you can modify "property descriptor"
    configurable: true,
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Make a child object of a parent object

A

var parent = {};

var child = Object.create(parent);

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

2 ways to access a property.

A

var o = { name: “Julie”, greet: function () { } };

o. name
o. greet()

o[“name”]
o“greet”

The latter is useful for situations like:
var meth = “greet”;
ometh;

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

Test if property is enumerable

A

Object.prototype.propertyIsEnumerable(“field-name”);

[].propertyIsEnumberable(“length”) //=> false

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

Two types of properties.

A

Data properties & accessor properties.

Data properties contain a value. Accessor properties contain a getter and/or setter function.

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

2 ways to create an accessor property

A
Literal Syntax:
{
  get getterProp() {
    return "J";
  },
  set setterProp(value) {
    console.log(value);
  }
}
ES5 Method:
Object.defineProperty(obj, "propName", {
  enumerable: true,
  configurable: true,
  get: function () { return "J"; },
  set: function (value) { console.log(value); }
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Convention to indicate that property should be treated like it’s private (even if it’s still public)

A

Prepend with underscore

_privProp

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

What 2 attributes do data & accessor properties share in common?

A

enumerable & configurable

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

What are the default attributes if you use the literal property assignment method: obj.name = "value"

A
enumerable = true
configurable = true
writable = true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the default property descriptor attributes if you use the ES5 Object.defineProperty method?

What about if the property already exists?

A

Every attribute not included in your property descriptor object will default to false.

However, if the property exists, only values included in the descriptor object will change the attribute values.

17
Q

Add/modify multiple properties to object using ES5 method

A

Object.defineProperties(obj, {
propName : { /* prop-descriptor */ },

});

18
Q

Get property’s attributes

A
Object.getOwnPropertyDescriptor(obj, "field-name")
// if data property => { enumerable, configurable, writable, value }
// if accessor property => { enumerable, configurable, set, get }
19
Q

How to prevent an object from receiving new attributes?

How to check if this occurred on an object?

A

Object.preventExtensions( obj )

// check if object is extensible
Object.isExtensible( obj )  //=> true|false
20
Q

What is a “sealed” object?

How to check if this occurred?

A

A “sealed” object means that you can only read or write to the object’s properties, you cannot add new properties and all existing properties become non-configurable.

Object.seal( obj )

// check if object is sealed
Object.isSealed( obj )
21
Q

What is a frozen object?

How to check if this occurred?

A

A “frozen” object is a sealed object + you cannot write to any properties. In essence, it’s a read only object. Frozen objects can not become unfrozen.

Object.freeze( obj )

// check if object is frozen
Object.isFrozen( obj )