Object Flashcards
Method returns a string representing the object.
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"
Method creates a new object, using an existing object to provide the newly created object’s __proto__ .
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"
Determines if an object is frozen.
const object1 = { property1: 42 };
console.log(Object.isFrozen(object1)); // expected output: false
Object.freeze(object1);
console.log(Object.isFrozen(object1)); // expected output: true
Method returns a string representing the object. This method is meant to be overridden by derived objects for locale-specific purposes.
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"
Method checks if an object exists in another object’s prototype chain.
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
Method prevents new properties from ever being added to an object (i.e. prevents future extensions to the object).
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 }
Method determines if an object is sealed.
const object1 = { property1: 42 };
console.log(Object.isSealed(object1)); // expected output: false
Object.seal(object1);
console.log(Object.isSealed(object1)); // expected output: true
Method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly upon a given object.
const object1 = { a: 1, b: 2, c: 3 };
console.log(Object.getOwnPropertyNames(object1)); // expected output: Array ["a", "b", "c"]
Method determines if an object is extensible (whether it can have new properties added to it).
const object1 = {};
console.log(Object.isExtensible(object1)); // expected output: true
Object.preventExtensions(object1);
console.log(Object.isExtensible(object1)); // expected output: false
Method defines new or modifies existing properties directly on an object, returning the object.
const object1 = {};
Object.defineProperties(object1, { property1: { value: 42, writable: true }, property2: {} });
console.log(object1.property1); // expected output: 42
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.
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
Method returns an array of all symbol properties found directly upon a given object.
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
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).
const object1 = { a: 'somestring', b: 42, c: false };
console.log(Object.values(object1)); // expected output: Array ["somestring", 42, false]
Defines a new property directly on an object, or modifies an existing property on an object, and returns the object.
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
Method returns an array of a given object’s own property names, in the same order as we get with a normal loop.
const object1 = { a: 'somestring', b: 42, c: false };
console.log(Object.keys(object1)); // expected output: Array ["a", "b", "c"]