CoreObject
CoreObject is a JavaScript library providing a lightweight, robust implementation of an OOP Class model. It was primarily developed for and used within the Ember-CLI ecosystem to define and extend classes, facilitating a structured approach to object-oriented programming in JavaScript. The package supports Node.js environments from version 4 onwards. While the latest public version is 3.1.5, its last major update was published approximately nine years ago, indicating that the project is currently abandoned. Consequently, there is no active release cadence, and it is not recommended for new projects seeking ongoing maintenance, security updates, or modern JavaScript features. Its primary value now lies in its historical context within legacy Ember-CLI applications, where it continues to function as a core utility.
Common errors
-
TypeError: CoreObject is not a constructor or undefined
cause Incorrect import statement or module resolution failure in ESM context for a CJS package.fixFor ESM, use `import CoreObject from 'core-object';`. For CJS, use `const CoreObject = require('core-object');`. Ensure your build/runtime environment correctly resolves CJS modules. -
TypeError: this._super is not a function
cause Attempting to call `this._super()` in a method that does not override a parent method, or `init` method did not properly call `this._super()` in the constructor chain.fixOnly call `this._super()` when truly overriding a parent method. In `init` methods, always include `this._super(...arguments)` as the first line. If a method is new, remove the `this._super()` call.
Warnings
- breaking The package is effectively abandoned, with its last public release (v3.1.5) over nine years ago. It receives no active maintenance, bug fixes, or security updates.
- gotcha Being an older CommonJS package, direct usage in pure ESM environments might require specific Node.js configuration or bundler setups to correctly interpret `require()` and `module.exports`. While Node.js often handles CJS imports in ESM, unexpected behaviors can arise, especially with tooling.
- gotcha The `init` method in `CoreObject` classes requires calling `this._super(...arguments)` to ensure proper initialization of parent classes, similar to `super()` in native ES6 classes. Forgetting this call can lead to uninitialized properties or unexpected behavior from inherited constructors.
Install
-
npm install core-object -
yarn add core-object -
pnpm add core-object
Imports
- CoreObject
import { CoreObject } from 'core-object';import CoreObject from 'core-object';
- CoreObject
const { CoreObject } = require('core-object');const CoreObject = require('core-object'); - BasicClass
class BasicClass extends require('core-object') {}class BasicClass extends CoreObject {}
Quickstart
import CoreObject from 'core-object';
// Define a base class
const Animal = CoreObject.extend({
init() {
this._super(...arguments);
this.name = 'Unnamed';
},
sayHello() {
console.log(`Hello, I am ${this.name}.`);
}
});
// Extend the base class
const Dog = Animal.extend({
init(name) {
this._super(...arguments);
this.name = name || 'Fido';
},
bark() {
console.log(`${this.name} says Woof!`);
},
sayHello() {
this._super(...arguments);
this.bark();
}
});
// Create instances
const genericAnimal = Animal.create();
genericAnimal.sayHello(); // Output: Hello, I am Unnamed.
const myDog = Dog.create('Buddy');
myDog.sayHello(); // Output: Hello, I am Buddy.\nBuddy says Woof!
myDog.bark(); // Output: Buddy says Woof!
// Demonstrating `reopen` for adding methods dynamically
Dog.reopen({
fetch() {
console.log(`${this.name} is fetching!`);
}
});
myDog.fetch(); // Output: Buddy is fetching!