Luca Del Puppo
Luca Del Puppo
Love sport: running, hiking
Love animals
const button =
document.createElement('button');
button.clicked = false;
button.addEventListener('click', () => {
button.clicked = true
});
writable
enumerable
configurable
const person = {};
Object.defineProperty(person, 'name',
{
value: 'Alice',
writable: false,
enumerable: true,
configurable: false
}
);
const animal = {};
Object.defineProperties(animal, {
name: {
value: 'Dog',
writable: true
},
sound: {
value: 'Woof',
enumerable: false
}
});
const myObj = { age: 30 };
const descriptor =
Object.getOwnPropertyDescriptor(myObj, 'age');
console.log(descriptor);
/* Output:
{
value: 30,
writable: true,
enumerable: true,
configurable: true
}
*/
Objects Property Descriptors are used when we want to define every single behaviour of the properties.
By default every attribute is set to true.
const button =
document.createElement('button');
Object.definedProperty(button, 'clicked', {
value: false,
writable: true,
enumerable: false,
configurable: false
})
button.addEventListener('click', () => {
button.clicked = true
});
const propSymbol = Symbol("propName");
const propSymbol2 = Symbol("propName");
propSymbol === propSymbol2 // false
const propSymbol = new Symbol("propName");
const nameSymbol = Symbol("name");
const surnameSymbol = Symbol("surname");
const person = {
name: 'John',
surname: 'Doe',
[nameSymbol]: "Jane",
[surnameSymbol]: "Blue"
}
// my-module-1.js
export const propSymbolShare = Symbol.for("propName");
// my-module-2.js
export const propSymbolShare = Symbol.for("propName");
// index.js
import { propSymbolShare as propSymbolShareM1 } from './my-module.js'
import { propSymbolShare as propSymbolShareM2 } from './my-module-2.js'
console.log(propSymbolShareM1 === propSymbolShareM2); // true
Well-known symbols are predefined global symbols that represent common behaviours in JavaScript, such as Symbol.iterator
, Symbol.asyncIterator
, Symbol.toStringTag
, and Symbol.species
.
function Range(start, end) {
this.start = start;
this.end = end;
this[Symbol.iterator] = function*() {
let current = this.start;
const last = this.end;
while (current <= last) {
yield current++;
}
}
return this;
}
for (let num of new Range(1, 10)) {
console.log(num);
}
const ClickedSymbol = Symbol('clicked')
const button =
document.createElement('button');
button[ClickedSymbol] = false
button.addEventListener('click', () => {
button[ClickedSymbol] = true
});
const weakmap = new WeakMap();
const key = { name: "John Doe" };
weakmap.set(key, "Some value");
const value = weakmap.get(key); // "Some value"
weakmap.delete(key); // true
weakmap.has(key); // false
WeakMap offers several practical applications in JavaScript:
Keys must be objects
No built-in iteration methods: such as forEach
or entries
.
No clear
method
const ClickedWeakMap = new WeakMap()
const button =
document.createElement('button');
ClickedWeakMap.set(button, false)
button.addEventListener('click', () => {
ClickedWeakMap.set(button, true)
});
const weakSet = new WeakSet();
const obj1 = {};
weakSet.add(obj1);
console.log(weakSet.has(obj1)); // true
weakSet.delete(obj1);
console.log(weakSet.has(obj1)); // false
WeakSet offers several practical applications in JavaScript:
Accepts only objects
No built-in iteration methods, such as forEach
etc. etc.
No clear
method
const ClickedWeakSet = new WeakSet()
const button =
document.createElement('button');
button.addEventListener('click', () => {
ClickedWeakSet.add(button)
});
const target = { age: 31 };
const handler = {
get(_target, prop) {
if (prop === "birthYear") {
return new Date().getFullYear() - _target['age'];
}
return _target[prop];
}
};
const proxy = new Proxy(target, handler);
console.log(proxy.birthYear); // 1992
const target = { age: 25 };
const handler = {
set(_target, prop, value) {
if (prop === "age"
&& (typeof value !== "number" || value < 0 || value > 150))
throw new TypeError("Invalid age value");
_target[prop] = value;
return true;
},
};
const proxy = new Proxy(target, handler);
proxy.age = 30; // Works fine
proxy.age = "thirty"; // Throws TypeError: "Invalid age value"
const handlers = {
set(t, p, v) {
if (p === 'clicked')
throw new Error('You cannot change this')
t[p] = v
}
}
const button = document.createElement('button')
button.addEventListener('click', () => {
button.clicked = true
});
const wrappedButton = new Proxy(button, handlers)
const numbers = [1, 2, 3];
const sum = (a, b, c) => a + b + c;
// Using Reflect.apply()
const result = Reflect.apply(sum, null, numbers);
console.log(result); // Output: 6
const obj = { x: 42, y: 'hello' };
// Using Reflect.get()
const value = Reflect.get(obj, 'x');
console.log(value); // Output: 42
const obj = { x: 42 };
const newObj = Object.create(obj);
// Using Reflect.set()
Reflect.set(newObj, 'x', 13);
console.log(newObj.x); // Output: 13
const obj = {};
// Using Reflect.defineProperty()
Reflect.defineProperty(obj, 'x', {
value: 42,
writable: false
});
console.log(obj.x); // Output: 42
const obj = { x: 42, [Symbol('key')]: 'symbolValue' };
// Using Reflect.ownKeys()
const keys = Reflect.ownKeys(obj);
console.log(keys); // Output: ['x', Symbol(key)]
Reflect
when there are other ways to manipulate and inspect JavaScript objects?Image by catalyststuff on Freepik
Image by catalyststuff on Freepik
Javascript you don't know
@puppo92
Luca Del Puppo
Puppo_92
@puppo