# object.js _object.js_ is a set of tools and abstractions to create and manage constructors, objects and prototype chains in idiomatic JavaScript. This is an alternative to the ES6 `class` syntax in JavaScript and provides several advantages: - _Uniform and minimalistic_ definition "syntax" based on basic JavaScript object literals. No special cases, special syntax or _"the same but slightly different"_ ways to do things, trying to adhere to [POLS](https://en.wikipedia.org/wiki/Principle_of_least_astonishment) as much as possible, - _Transparently_ based on JavaScript's prototypical inheritance model, - Produces fully introspectable constructors/instances, - Does not try to emulate constructs foreign to JavaScript (i.e. classes), - Granular 2-stage instance construction and initialization (a-la _Python's_ `.__new__(..)` and `.__init__(..)` methods), - Simple way to define callable instances (including a-la _Python's_ `.__call__(..)`), - Less restrictive: - `new` is optional, - all input components are reusable JavaScript objects, - no artificial restrictions. Disadvantages compared to the `class` syntax: - No _syntactic sugar_, - Slightly more complicated calling of `parent` (_super_) methods. Note that the produced constructors and objects are functionally identical (almost) to the ones produced via ES6 classes and are interchangeable with them. Here is a basic comparison:
| _object.js_ ```javascript var A = object.Constructor('A', { // prototype attribute (inherited)... attr: 'prototype', method: function(){ // ... }, }) var B = object.Constructor('B', A, { constructor_attr: 'constructor', constructor_method: function(){ return 'constructor' }, }, { get prop(){ return 42 }, __init__: function(){ this.instance_attr = 7 }, }) ``` - No _direct_ way to do "private" definitions, - Clear separation of constructor and `.prototype` For example, in `B`: - First block (optional) is merged with `B`, - Second block _is_ the `B.prototype`, - No special syntax, stands out less. | _ES6_ ```javascript class A { // instance attribute (copied)... attr = 'instance' method(){ // ... } } class B extends A { static constructor_attr = 'class' static constructor_method(){ return 'class' } get prop(){ return 42 } constructor(){ super(...arguments) this.instance_attr = 7 } } ``` - Syntax pretty but _misleading_; calling a _constructor_ a class is not correct, - `static` and instance definitions are not separated, - lots of details done non-transparently under the hood. |