Published
Edited
Jan 16, 2021
Insert cell
md`# Objects, Inheritance;`
Insert cell
{
{};
new Object();
//4 '';
(4).toFixed(2);
}
Insert cell
{
const a = new String('asd');
return typeof a;
}
Insert cell
{
const a = new String('asd');
return a instanceof String;
}
Insert cell
{
a.__proto__ === String.prototype; //false
a.__proto__.__proto__ === String.prototype;
// false
}
Insert cell
{
Object.create(null); // __proto__ is arument of .create
}
Insert cell
{
function Car(color) {
this.color = color;
}
Car.prototype.beep = function __beep() {
return 'beeeeep';
}
function Lada(color) {
Car.call(this, color); // ~super
}
Lada.prototype = Object.create(Car.prototype); // __proto__ === Car.prototype
Lada.prototype.constructor = Lada; // redundant
const a = new Lada();
return a.beep();
}
Insert cell
{
class Car {
constructor(color) {
this.color = color;
}
beep() {
return 'beep';
}
}
class Lada extends Car {
constructor(color) {
super(color);
this.model = 'kalina';
}
}
const a = new Lada('red');
return a;
}
Insert cell
{
function A() {}
A.fun = function() {};
class B {
static fun() {
}
}
}
Insert cell
{
class Poster {
static ORIENTATION = {
LANDSCAPE: 'landscape',
PORTRAIT: 'portrait'
}
constructor() {
this.orientation = Poster.ORIENTATION.LANDSCAPE;
}
}
}
Insert cell
{
Object.keys({}); // list key as array;
const a = { a };
Object.keys(a).map((key) => a[key])
Object.values({}); // list all values as an array
return Object.entries({}); // [[key, value]]
}
Insert cell
md`Copy object`
Insert cell
{
// only top level
function copy1(obj) {
return Object.assign({}, obj);
}
// only top level
const copy2 = (o) => ({ ...o });
function copy3(o) {
return JSON.parse(JSON.stringify(o)); // will lost any function
}
// only top level
function copy4 (o) {
const newObject = {};
for (let key in o) {
newObject[key] = o[key];
}
}
}
Insert cell
md`Deep copy`
Insert cell
// only top level
function deepCopy (o) {
const newObject = Object.create(o.__proto__);
for (let key in o) {
const value = o[key];
if (null !== value && typeof value === 'object') {
newObject[key] = deepCopy(value);
}
newObject[key] = o[key];
}
}
Insert cell
md`Unique`
Insert cell
{
const a = [1, 2, 3, 1, 2, 3, 1, 2, 3];
// function unique(a) {
// return Array.from(new Set(a))
// }
// function unique(a) {
// return a.filter((e, i) => a.indexOf(e) === i);
// }

function unique(a) {
const map = a.reduce((R, i) => {
R[i] = i;
return R;
}, {});
return Object.values(map);
}
return unique(a) // [1, 2, 3];
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more