Please enable JavaScript.
Coggle requires JavaScript to display documents.
Objects and Inheritance (Single Objects (Kinds of Properties, Object…
Objects and Inheritance
Single Objects
Kinds of Properties
Object Literals
Dot Operator (.): Accessing Properties via Fixed Keys
Unusual Property Keys
Bracket Operator ([]): Accessing Properties via Computed Keys
Converting Any Value to an Object
this as an Implicit Parameter of Functions
and Methods
Calling Functions While Setting this: call(), apply(), and bind()
Pitfall: Losing this When Extracting a Method
Pitfall: Functions Inside Methods Shadow this
Layer 2:
The Prototype Relationship
Between Objects
Getting and Setting the Prototype
create new object : Object.create(proto, propDescObj?)
ex:
var jane = Object.create(PersonProto, {
name: { value: 'Jane', writable: true }
});
Reading the prototype of an object: Object.getPrototypeOf(obj)
Setting and Deleting Affects Only Own Properties
Iteration and
Detection of Properties
Listing Own Property Keys
Object.getOwnPropertyNames(obj): return keys of all own properties of obj
Object.keys() : epepecially for object taht you have create
Checking Whether a Property Exists
Listing All Property Keys
option one : for («variable» in «object»)
«statement»
option two:
function getAllPropertyNames(obj) {
var result = [];
while (obj) {
// Add the own property names of
obj
to
result
Array.prototype.push.apply(result, Object.getOwnPropertyNames(obj));
obj = Object.getPrototypeOf(obj);
}
return result;
}
Checking Whether a Property Exists
propKey in obj
Object.prototype.hasOwnProperty
Avoid invoking hasOwnProperty() directly on an object, as it may
be overridden
not use: obj.hasOwnProperty('foo')
use: Object.prototype.hasOwnProperty.call(obj, 'foo')
or:{}.hasOwnProperty.call(obj, 'foo')
Accessors (Getters and Setters)
Property Attributes and
Property Descriptors
Property Attributes
Getting and Defining Properties via Descriptors
Protecting Objects
Object.preventExtensions(obj)
You check whether an object is extensible via:
Object.isExtensible(obj)
Protecting Obje
Sealing
You check whether an object is sealed via:
Object.isSealed(obj)
Layer 3:
Constructors—Factories for Instances
Terminology: The Two Prototypes
The constructor Property of Instances
The instanceof Operator:
value instanceof Constr
Data in Prototype Properties
Polymorphic Prototype Properties
Keeping Data Private
Private Data in Properties with Marked Keys
Keeping Global Data Private via IIFEs
Layer 4: Inheritance Between Constructors
Inheriting Instance Properties
Methods of All Objects
Object.prototype.toLocaleString()
Generic Methods: Borrowing Methods from Prototypes
Array
'abc'.split('').map(function (x) { return x.toUpperCase() })
Array.prototype.join.call('abc', '-')
[].map.call('abc', function (x) { return x.toUpperCase() })
A List of All Generic Methods
Pitfalls: Using an Object as a Map
Pitfall 1: Inheritance Affects Reading Properties