Created
January 13, 2019 17:57
-
-
Save dariusz-wozniak/b0eb040c4f5823800957f16156aaf9b5 to your computer and use it in GitHub Desktop.
π JavaScript Objects and Prototypes (Pluralsight course) β Notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Readonly property: | |
// throws TypeError when 'use strict'; | |
var cat = { | |
name: 'Fluffy' | |
}; | |
Object.defineProperty(cat, 'name', {writable: false}); | |
// writable | |
// enumerable | |
// configurable | |
cat.name = 'zzz'; | |
console.log(cat.name); // Fluffy | |
// Property with getter-setter (getter / setter are optional): | |
Object.defineProperty(cat, 'fullName',{ | |
get: function() { | |
return this.name.first + ' ' + this.name.last | |
}, | |
set: function(value) { | |
var nameParts = value.split(' '); | |
this.name.first = nameParts[0]; | |
this.name.last = nameParts[1]; | |
} | |
}) | |
// Prototypes (I haven't compiled it): | |
/* | |
A function's prototype: | |
IS the object instance that will become the prototype for all objects created using this function as a constructor. | |
An object's prototype: | |
IS the object instance from which the object is inherited. | |
*/ | |
function Cat() {} | |
Cat.prototype.age = 4; | |
var fluffy = new Cat(); | |
fluffy.age = 5; | |
display(fluffy.age); // 5 | |
display(fluffy.__proto__.age) // 4 | |
// More on prototypes: | |
fluffy.hasOwnProprtty('age') | |
fluffy.__proto__.age = 4 | |
Cat.prototype.constructor = Cat | |
x.__proto__.__proto__ //... | |
// MORE: | |
// ==================== | |
// functions are also properties | |
Object.create | |
Object.configurable | |
Object.freeze | |
Object.keys | |
delete cat.name | |
x instanceof y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment