温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/util/inherits
点击访问原文链接

Node util.inherits function | API Reference | Bun

Node util.inherits function | API Reference | BunBuildDocsReferenceGuidesBlogDiscord/node:util/inheritsFinherits

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:util/inheritsFinherits

function

util.inherits { console.log(`Received data: "${data}"`); }); stream.write('It works!'); // Received data: "It works!" ``` ES6 example using `class` and `extends`: ```js import EventEmitter from 'node:events'; class MyStream extends EventEmitter { write(data) { this.emit('data', data); } } const stream = new MyStream(); stream.on('data', (data) => { console.log(`Received data: "${data}"`); }); stream.write('With ES6'); ```" data-algolia-static="false" data-algolia-merged="false" data-type="Function">function inherits(constructor: unknown,superConstructor: unknown): void;

Usage of util.inherits() is discouraged. Please use the ES6 class and extends keywords to get language level inheritance support. Also note that the two styles are semantically incompatible.

Inherit the prototype methods from one constructor into another. The prototype of constructor will be set to a new object created from superConstructor.

This mainly adds some input validation on top of Object.setPrototypeOf(constructor.prototype, superConstructor.prototype). As an additional convenience, superConstructor will be accessible through the constructor.super_ property.

const util = require('node:util');
const EventEmitter = require('node:events');

function MyStream() {
EventEmitter.call(this);
}

util.inherits(MyStream, EventEmitter);

MyStream.prototype.write = function(data) {
this.emit('data', data);
};

const stream = new MyStream();

console.log(stream instanceof EventEmitter); // true
console.log(MyStream.super_ === EventEmitter); // true

stream.on('data', (data) => {
console.log(`Received data: "${data}"`);
});
stream.write('It works!'); // Received data: "It works!"

ES6 example using class and extends:

import EventEmitter from 'node:events';

class MyStream extends EventEmitter {
write(data) {
this.emit('data', data);
}
}

const stream = new MyStream();

stream.on('data', (data) => {
console.log(`Received data: "${data}"`);
});
stream.write('With ES6');

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

Node util.inherits function | API Reference | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for function node:util.inherits | Bun