Objects Flashcards
1
Q
Object.length
A
Has a value of 1
2
Q
Object.prototype
A
- Allows addition of properties to all objects of type
Object
3
Q
Object.assign()
A
Object Constructor Methods
Object.assign(target, ...sources)
Description
- Used to copy the values of all enumerable own properties from one or more
source
objects to atarget
object. - Properties in the
target
object will be overwritten by properties in the sources if they have the same key. Later sources’ properties will similarly overwrite earlier ones.
Parameters
-
target
- the target object -
sources
- the source object(s)
Notes
- Uses
[[Get]]
on the sources and[[Set]]
on the target, so it will invoke getters and setters. -
Assigns properties versus just copying or defining new properties (aka the entire definition including getters). For copying property definitions, including enumerability, into prototypes, use
Object.getOwnPropertyDescriptor()
andObject.defineProperties()
. - Both
String
andSymbol
properties are copied - Primitives are wrapped to object
- Only strings are enumerable though.
- Deep clone an object: (use the following in an
Object.assign
source)JSON.parse(JSON.stringify(obj))
- If a property on the target is read-only, and a source tries to overwrite it,
TypeError
will be thrown - Exceptions will interrupt the ongoing copying task (not atomic transactions)
- To copy whole descriptor, not just simple assignment, must go through object’s keys and symbols and use
getOwnPropertyDescriptor
Thoughts
- Use
Object.assign(this, {x, y})
inconstructor
to quickly setup instance properties - Instead of
MyClass.prototype.foo
, can useassign()
instead:
Object.assign(MyClass.prototype, {
` foo(arg1, arg2) {
//…
}
}`
- Shallow copy (including prototype):
let newObj = Object.assign({\_\_proto\_\_: obj.\_\_proto\_\_}, obj);
- Major performance pitfalls using this
- Use
Object.create()
to get desired[[Prototype]]
instead
- If shallow copying only own properties:
let newObj = Object.assign({}, obj);
4
Q
Object.create()
A
Object Constructor Methods
Object.create(proto[, propertiesObject])
Description
- Creates a new object with the specified prototype and properties
Parameters
-
proto
- The object which should be the prototype of the newly-created object -
propertiesObject
- property descriptors to be added to the newly-created object, with the corresponding property names. These properties correspond to the second argument ofObject.defineProperties()
Return
- A new object with the specified prototype and properties
Exceptions
-
TypeError
thrown ifpropertiesObject
isn’t null or an object
Notes
- Classical inheritance using functions as classes
Rectangle.prototype = Object.create(Shape.prototype);Rectangle.prototype.constructor = Rectangle;
- Can achieve quasi-multiple-inheritance using mixins
MyClass.prototype = Object.create(SuperClass.prototype);Object.assign(MyClass.prototype, OtherSuperClass.prototype);MyClass.prototype.constructor = MyClass;
-
o = {}
equivalent too = Object.create(Object.prototype)
- By default properties are not writable, enumerable, or configurable
5
Q
A