Object Flashcards

1
Q

Method returns a string representing the object.

A
function Dog(name) {
  this.name = name;
}

dog1 = new Dog(‘Gabby’);

Dog.prototype.toString = function dogToString() {
return this.name;
}

console.log(dog1.toString());
// expected output: "Gabby"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Method creates a new object, using an existing object to provide the newly created object’s __proto__ .

A
const person = {
  isHuman: false,
  printIntroduction: function () {
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
  }
};

const me = Object.create(person);

me. name = “Matthew”; // “name” is a property set on “me”, but not on “person”
me. isHuman = true; // inherited properties can be overwritten

me.printIntroduction();
// expected output: "My name is Matthew. Am I human? true"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Determines if an object is frozen.

A
const object1 = {
  property1: 42
};
console.log(Object.isFrozen(object1));
// expected output: false

Object.freeze(object1);

console.log(Object.isFrozen(object1));
// expected output: true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Method returns a string representing the object. This method is meant to be overridden by derived objects for locale-specific purposes.

A

const date1 = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

console.log(date1.toLocaleString('ar-EG'));
// expected output: "٢٠‏/١٢‏/٢٠١٢ ٤:٠٠:٠٠ ص"

const number1 = 123456.789;

console.log(number1.toLocaleString('de-DE'));
// expected output: "123.456,789"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Method checks if an object exists in another object’s prototype chain.

A
function object1() {}
function object2() {}

object1.prototype = Object.create(object2.prototype);

const object3 = new object1();

console.log(object1.prototype.isPrototypeOf(object3));
// expected output: true
console.log(object2.prototype.isPrototypeOf(object3));
// expected output: true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Method prevents new properties from ever being added to an object (i.e. prevents future extensions to the object).

A

const object1 = {};

Object.preventExtensions(object1);

try {
  Object.defineProperty(object1, 'property1', {
    value: 42
  });
} catch (e) {
  console.log(e);
  // Expected output: TypeError: Cannot define property property1, object is not extensible
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Method determines if an object is sealed.

A
const object1 = {
  property1: 42
};
console.log(Object.isSealed(object1));
// expected output: false

Object.seal(object1);

console.log(Object.isSealed(object1));
// expected output: true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly upon a given object.

A
const object1 = {
  a: 1,
  b: 2,
  c: 3
};
console.log(Object.getOwnPropertyNames(object1));
// expected output: Array ["a", "b", "c"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Method determines if an object is extensible (whether it can have new properties added to it).

A

const object1 = {};

console.log(Object.isExtensible(object1));
// expected output: true

Object.preventExtensions(object1);

console.log(Object.isExtensible(object1));
// expected output: false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Method defines new or modifies existing properties directly on an object, returning the object.

A

const object1 = {};

Object.defineProperties(object1, {
  property1: {
    value: 42,
    writable: true
  },
  property2: {}
});
console.log(object1.property1);
// expected output: 42
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

A
const object1 = {
  a: 1,
  b: 2,
  c: 3
};

const object2 = Object.assign({c: 4, d: 5}, object1);

console.log(object2.c, object2.d);
// expected output: 3 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Method returns an array of all symbol properties found directly upon a given object.

A
const object1 = {};
const a = Symbol('a');
const b = Symbol.for('b');
object1[a] = 'localSymbol';
object1[b] = 'globalSymbol';

const objectSymbols = Object.getOwnPropertySymbols(object1);

console.log(objectSymbols.length);
// expected output: 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Method returns an array of a given object’s own enumerable property values, in the same order as that provided by a for…in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

A
const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};
console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

A

const object1 = {};

Object.defineProperty(object1, ‘property1’, {
value: 42,
writable: false
});

object1.property1 = 77;
// throws an error in strict mode
console.log(object1.property1);
// expected output: 42
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Method returns an array of a given object’s own property names, in the same order as we get with a normal loop.

A
const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};
console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

A
const object1 = new Object();
object1.property1 = 42;
console.log(object1.hasOwnProperty('property1'));
// expected output: true
console.log(object1.hasOwnProperty('toString'));
// expected output: false
console.log(object1.hasOwnProperty('hasOwnProperty'));
// expected output: false
17
Q

Method returns all own property descriptors of a given object.

A
const object1 = {
  property1: 42
};

const descriptors1 = Object.getOwnPropertyDescriptors(object1);

console.log(descriptors1.property1.writable);
// expected output: true
console.log(descriptors1.property1.value);
// expected output: 42
18
Q

Method determines whether two values are the same value.

A
var test = { a: 1 };
Object.is(test, test);       // true

Object.is(null, null); // true

// Special Cases
Object.is(0, -0);            // false
Object.is(-0, -0);           // true
Object.is(NaN, 0/0);         // true
19
Q

Method returns a Boolean indicating whether the specified property is enumerable.

A
const object1 = {};
const array1 = [];
object1.property1 = 42;
array1[0] = 42;
console.log(object1.propertyIsEnumerable('property1'));
// expected output: true
console.log(array1.propertyIsEnumerable(0));
// expected output: true
console.log(array1.propertyIsEnumerable('length'));
// expected output: false
20
Q

Method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. In addition, freezing an object also prevents its prototype from being changed. freeze() returns the same object that was passed in.

A
const object1 = {
  property1: 42
};

const object2 = Object.freeze(object1);

object2.property1 = 33;
// Throws an error in strict mode
console.log(object2.property1);
// expected output: 42
21
Q

Method returns an array of a given object’s own enumerable property [key, value] pairs, in the same order as that provided by a for…in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

A
const object1 = { foo: 'bar', baz: 42 };
console.log(Object.entries(object1)[1]);
// expected output: Array ["baz", 42]
const object2 = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.entries(object2)[2]);
// expected output: Array ["2", "c"]
const object3 = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.entries(object3)[0]);
// expected output: Array ["2", "b"]
22
Q

Method returns a property descriptor for an own property (that is, one directly present on an object and not in the object’s prototype chain) of a given object.

A
const object1 = {
  property1: 42
}

const descriptor1 = Object.getOwnPropertyDescriptor(object1, ‘property1’);

console.log(descriptor1.configurable);
// expected output: true
console.log(descriptor1.value);
// expected output: 42
23
Q

Method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.

A
const prototype1 = {};
const object1 = Object.create(prototype1);
console.log(Object.getPrototypeOf(object1) === prototype1);
// expected output: true
24
Q

Method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.

A
const object1 = {
  property1: 42
};

Object.seal(object1);
object1.property1 = 33;
console.log(object1.property1);
// expected output: 33

delete object1.property1; // cannot delete when sealed
console.log(object1.property1);
// expected output: 33
25
Q

Method transforms a list of key-value pairs into an object.

A

const entries = new Map([
[‘foo’, ‘bar’],
[‘baz’, 42]
]);

const obj = Object.fromEntries(entries);

console.log(obj);
// expected output: Object { foo: "bar", baz: 42 }
26
Q

Method returns the primitive value of the specified object.

A
function MyNumberType(n) {
  this.number = n;
}

MyNumberType.prototype.valueOf = function() {
return this.number;
};

const object1 = new MyNumberType(4);

console.log(object1 + 3);
// expected output: 7