or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-setprototypeof

A small polyfill for Object.setPrototypeOf with cross-platform compatibility

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/setprototypeof@1.2.x

To install, run

npx @tessl/cli install tessl/npm-setprototypeof@1.2.0

index.mddocs/

SetPrototypeOf

SetPrototypeOf 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.

Package Information

  • Package Name: setprototypeof
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install setprototypeof
  • TypeScript Support: Included via index.d.ts

Core Imports

const setPrototypeOf = require('setprototypeof');

For TypeScript and modern environments:

import setPrototypeOf from 'setprototypeof';

Basic Usage

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"

Architecture

SetPrototypeOf implements an intelligent fallback strategy to ensure cross-platform compatibility:

  1. Native Method: Uses Object.setPrototypeOf when available (modern browsers and Node.js)
  2. Proto Property: Falls back to __proto__ property assignment for environments that support it
  3. Property Mixing: Final fallback that copies properties from prototype to object for legacy environments (IE8+)

The library automatically detects which method to use at runtime, providing seamless operation across all JavaScript environments.

Capabilities

Set Prototype

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 modified
  • proto (object | null): The prototype object to set, or null to remove prototype

Returns:

  • any: The same object reference that was passed in (modified in place)

Implementation Details:

  • Uses native Object.setPrototypeOf when available
  • Falls back to __proto__ property assignment in compatible environments
  • Uses property copying for legacy browser support (IE8+)
  • Automatically detects the best method at runtime

Usage 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); // 2

Environment Compatibility

  • Modern Browsers: Chrome 34+, Firefox 31+, Safari 9+, Edge (all versions)
  • Node.js: All versions
  • Legacy Browsers: Internet Explorer 8+, older mobile browsers
  • Fallback Behavior: Gracefully degrades functionality while maintaining API compatibility

Error Handling

The function follows the same error handling patterns as the native Object.setPrototypeOf:

  • Throws TypeError if the object is not extensible (in strict environments)
  • Throws TypeError if prototype would create a circular reference
  • In legacy fallback mode, some errors may not be thrown but prototype setting will fail silently

TypeScript Integration

Full 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"