A small polyfill for Object.setPrototypeOf with cross-platform compatibility
npx @tessl/cli install tessl/npm-setprototypeof@1.2.0SetPrototypeOf is a lightweight polyfill library that provides a cross-platform implementation of Object.setPrototypeOf functionality. It automatically detects the best available method for setting object prototypes, ensuring compatibility across different JavaScript environments from modern browsers to legacy systems like IE8.
npm install setprototypeofconst setPrototypeOf = require('setprototypeof');For TypeScript and modern environments:
import setPrototypeOf from 'setprototypeof';const setPrototypeOf = require('setprototypeof');
// Create a base object with methods
const animal = {
speak: function() {
return 'Some sound';
}
};
// Create an instance object
const dog = {
name: 'Buddy'
};
// Set the prototype to establish inheritance
setPrototypeOf(dog, animal);
// Now dog can access methods from animal
console.log(dog.speak()); // "Some sound"
console.log(dog.name); // "Buddy"SetPrototypeOf implements an intelligent fallback strategy to ensure cross-platform compatibility:
Object.setPrototypeOf when available (modern browsers and Node.js)__proto__ property assignment for environments that support itThe library automatically detects which method to use at runtime, providing seamless operation across all JavaScript environments.
Sets the prototype of an object using the best available method for the current environment.
/**
* Sets the prototype of an object using cross-platform compatible method
* @param o - The object whose prototype will be set
* @param proto - The new prototype object or null
* @returns The modified object (same reference as input o)
*/
function setPrototypeOf(o: any, proto: object | null): any;Parameters:
o (any): The target object whose prototype will be modifiedproto (object | null): The prototype object to set, or null to remove prototypeReturns:
any: The same object reference that was passed in (modified in place)Implementation Details:
Object.setPrototypeOf when available__proto__ property assignment in compatible environmentsUsage Examples:
const setPrototypeOf = require('setprototypeof');
// Basic prototype setting
const parent = { x: 10 };
const child = { y: 20 };
setPrototypeOf(child, parent);
console.log(child.x); // 10 (inherited from parent)
// Setting null prototype
const isolated = { data: 'value' };
setPrototypeOf(isolated, null);
console.log(Object.getPrototypeOf(isolated)); // null
// Chaining (returns the same object)
const result = setPrototypeOf({ a: 1 }, { b: 2 });
console.log(result.a); // 1
console.log(result.b); // 2The function follows the same error handling patterns as the native Object.setPrototypeOf:
TypeError if the object is not extensible (in strict environments)TypeError if prototype would create a circular referenceFull TypeScript support is provided via included type definitions:
import setPrototypeOf from 'setprototypeof';
interface Animal {
speak(): string;
}
interface Dog extends Animal {
name: string;
}
const animal: Animal = {
speak: () => 'woof'
};
const dog = { name: 'Buddy' };
setPrototypeOf(dog, animal);
// TypeScript will properly infer the extended interface
const typedDog = dog as Dog;
console.log(typedDog.speak()); // "woof"