or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--create

Creates objects that inherit from a given prototype with optional property assignments

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.create@3.1.x

To install, run

npx @tessl/cli install tessl/npm-lodash--create@3.1.0

index.mddocs/

Lodash Create

The modern build of lodash's _.create as a standalone module. Creates objects that inherit from a given prototype with optional property assignments, enabling efficient prototypal inheritance patterns in JavaScript.

Package Information

  • Package Name: lodash.create
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.create

Core Imports

const create = require("lodash.create");

Basic Usage

const create = require("lodash.create");

// Simple prototype inheritance
function Shape() {
  this.x = 0;
  this.y = 0;
}

function Circle() {
  Shape.call(this);
}

// Create prototype chain with additional properties
Circle.prototype = create(Shape.prototype, {
  'constructor': Circle
});

const circle = new Circle();
console.log(circle instanceof Circle); // => true
console.log(circle instanceof Shape);  // => true

Capabilities

Object Creation with Prototypal Inheritance

Creates an object that inherits from the given prototype object with optional property assignments.

/**
 * Creates an object that inherits from the given `prototype` object. If a
 * `properties` object is provided its own enumerable properties are assigned
 * to the created object.
 *
 * @param {Object} prototype The object to inherit from.
 * @param {Object} [properties] The properties to assign to the object.
 * @param {Object} [guard] Enables use as a callback for functions like `_.map`.
 * @returns {Object} Returns the new object.
 */
function create(prototype, properties, guard)

Parameters:

  • prototype (Object): The object to inherit from
  • properties (Object, optional): The properties to assign to the object
  • guard (Object, optional): Internal parameter - enables use as a callback for functions like _.map.

Returns: Object - Returns the new object that inherits from the prototype

Usage Examples:

const create = require("lodash.create");

// Basic prototype inheritance
const animal = {
  type: 'animal',
  speak() {
    console.log('Some generic animal sound');
  }
};

const dog = create(animal, {
  type: 'dog',
  speak() {
    console.log('Woof!');
  },
  breed: 'labrador'
});

console.log(dog.type);   // => 'dog'
console.log(dog.breed);  // => 'labrador'
dog.speak();             // => 'Woof!'

// Constructor function pattern
function Vehicle(wheels) {
  this.wheels = wheels;
}

Vehicle.prototype.move = function() {
  return 'Moving with ' + this.wheels + ' wheels';
};

function Car() {
  Vehicle.call(this, 4);
}

// Set up inheritance chain
Car.prototype = create(Vehicle.prototype, {
  constructor: Car,
  honk() {
    return 'Beep beep!';
  }
});

const myCar = new Car();
console.log(myCar.move());  // => 'Moving with 4 wheels'
console.log(myCar.honk());  // => 'Beep beep!'
console.log(myCar instanceof Car);     // => true
console.log(myCar instanceof Vehicle); // => true

// Creating objects without properties
const emptyChild = create(animal);
console.log(emptyChild.type); // => 'animal' (inherited)
emptyChild.speak();           // => 'Some generic animal sound'

// Creating from null prototype (no inherited properties)
const cleanObject = create(null, {
  name: 'clean',
  value: 42
});

console.log(cleanObject.name);           // => 'clean'
console.log(cleanObject.toString);       // => undefined (no Object.prototype)
console.log(cleanObject.hasOwnProperty); // => undefined (no Object.prototype)