| Signature | Brief description | Example |
|---|---|---|
Static methods (on Object) | ||
Object.assign(target, ...sources) | Shallow copy / merge | Object.assign({}, a, b) |
Object.create(proto[, props]) | Create with prototype | const o = Object.create(null) |
Object.defineProperty(obj, key, desc) | Define 1 property | Object.defineProperty(o,'id',{value:1,writable:false}) |
Object.defineProperties(obj, descs) | Define many | Object.defineProperties(o,{a:{value:1},b:{value:2}}) |
Object.entries(obj) | [[key,value]] array | Object.entries(o).forEach(([k,v]) => ...) |
Object.fromEntries(iter) | Entries → object | Object.fromEntries(new URLSearchParams(qs)) |
Object.values(obj) | Enumerable values | Object.values(o).includes(42) |
Object.keys(obj) | Enumerable keys | for (const k of Object.keys(o)) { ... } |
Object.getOwnPropertyNames(obj) | Own string keys (incl. non-enum) | Object.getOwnPropertyNames(o) |
Object.getOwnPropertySymbols(obj) | Own symbol keys | Object.getOwnPropertySymbols(o) |
Object.getOwnPropertyDescriptor(obj, key) | Descriptor of key | Object.getOwnPropertyDescriptor(o,'x') |
Object.getOwnPropertyDescriptors(obj) | All descriptors | Object.getOwnPropertyDescriptors(o) |
Object.getPrototypeOf(obj) | Read prototype | Object.getPrototypeOf(o) === Foo.prototype |
Object.setPrototypeOf(obj, proto) | Set prototype | Object.setPrototypeOf(o, null) |
Object.is(a, b) | SameValue compare | Object.is(NaN, NaN) === true |
Object.seal(obj) | Block add/remove props | Object.seal(o) |
Object.isSealed(obj) | Sealed? | Object.isSealed(o) |
Object.freeze(obj) | Make immutable (shallow) | Object.freeze(config) |
Object.isFrozen(obj) | Frozen? | Object.isFrozen(config) |
Object.preventExtensions(obj) | Disallow new props | Object.preventExtensions(o) |
Object.isExtensible(obj) | Extensible? | Object.isExtensible(o) |
Object.hasOwn(obj, key) | Own key present? | Object.hasOwn(o, 'id') |
Prototype methods (on Object.prototype) | ||
obj.hasOwnProperty(key) | Own key check | o.hasOwnProperty('x') |
obj.propertyIsEnumerable(key) | Enumerable? | o.propertyIsEnumerable('x') |
obj.isPrototypeOf(value) | In prototype chain? | Foo.prototype.isPrototypeOf(inst) |
obj.toString() | Tag / legacy string | Object.prototype.toString.call([]) // "[object Array]" |
obj.toLocaleString() | Locale string | date.toLocaleString('en-US') |
obj.valueOf() | Primitive hint | new Number(5).valueOf() === 5 |
| Prototype properties / accessors | ||
obj.__proto__ (getter/setter) | Legacy proto access | obj.__proto__ = null (prefer getPrototypeOf) |
obj.constructor | Ref to constructor | o.constructor === Object |
// ── Basic class with fields, methods, getter/setter, static ──────────
class Point {
x = 0; // public instance field (per instance)
y = 0;
constructor(x, y) { // runs on `new Point(...)`
this.x = x; this.y = y;
}
length() { return Math.hypot(this.x, this.y); } // prototype method
get r() { return this.length(); } // getter
set r(v) {
const a = Math.atan2(this.y, this.x);
this.x = v * Math.cos(a); this.y = v * Math.sin(a);
}
static origin() { return new Point(0, 0); } // static method
}
// ── Private fields and methods ───────────────────────────────────────
class Counter {
#value = 0; // private instance field
static #MAX = 1_000_000; // private static field
get value() { return this.#value; }
#clamp(n) { return Math.min(n, Counter.#MAX); } // private method
increment() { this.#value = this.#clamp(this.#value + 1); }
static from(n) { const c = new Counter(); c.#value = c.#clamp(n); return c; }
}
// ── Inheritance ───────────────────────────────────────────────────────
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a noise.`; }
}
class Dog extends Animal {
constructor(name) { super(name); } // must call super() before `this`
speak() { return `${super.speak()} Woof!`; } // override + call base
}
// ── Static blocks (run once when class is evaluated) ─────────────────
class Config {
static base = '/api';
static #token;
static {
this.#token = crypto.randomUUID?.() ?? 'dev-token';
}
static authHeader() { return { Authorization: `Bearer ${this.#token}` }; }
}
// ── Computed method names ─────────────────────────────────────────────
class Greeter {
['say' + 'Hi']() { return 'hi'; }
static ['v' + 1] = 2; // computed static field
}
// ── Abstract-like base ────────────────────────────────────────────────
class Base {
constructor() {
if (new.target === Base) throw new TypeError('Abstract');
}
}
// ── Arrow method keeps `this` bound without .bind() ──────────────────
class Button {
count = 0;
handleClick = (e) => { this.count++; }; // bound arrow as field
}