├── .github └── workflows │ ├── ci.yml │ ├── commit-if-modified.sh │ ├── copyright-year.sh │ ├── isaacs-makework.yml │ └── package-json-repo.js ├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── inherits.js ├── inherits_browser.js ├── package-lock.json ├── package.json └── test ├── browser.js ├── builtin.js └── old.js /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | strategy: 8 | matrix: 9 | node-version: [12.x, 14.x, 16.x, 17.x] 10 | platform: 11 | - os: ubuntu-latest 12 | shell: bash 13 | - os: macos-latest 14 | shell: bash 15 | - os: windows-latest 16 | shell: bash 17 | - os: windows-latest 18 | shell: powershell 19 | fail-fast: false 20 | 21 | runs-on: ${{ matrix.platform.os }} 22 | defaults: 23 | run: 24 | shell: ${{ matrix.platform.shell }} 25 | 26 | steps: 27 | - name: Checkout Repository 28 | uses: actions/checkout@v1.1.0 29 | 30 | - name: Use Nodejs ${{ matrix.node-version }} 31 | uses: actions/setup-node@v1 32 | with: 33 | node-version: ${{ matrix.node-version }} 34 | 35 | - name: Install dependencies 36 | run: npm install 37 | 38 | - name: Run Tests 39 | run: npm test -- -c -t0 40 | -------------------------------------------------------------------------------- /.github/workflows/commit-if-modified.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | git config --global user.email "$1" 3 | shift 4 | git config --global user.name "$1" 5 | shift 6 | message="$1" 7 | shift 8 | if [ $(git status --porcelain "$@" | egrep '^ M' | wc -l) -gt 0 ]; then 9 | git add "$@" 10 | git commit -m "$message" 11 | git push || git pull --rebase 12 | git push 13 | fi 14 | -------------------------------------------------------------------------------- /.github/workflows/copyright-year.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | dir=${1:-$PWD} 3 | dates=($(git log --date=format:%Y --pretty=format:'%ad' --reverse | sort | uniq)) 4 | if [ "${#dates[@]}" -eq 1 ]; then 5 | datestr="${dates}" 6 | else 7 | datestr="${dates}-${dates[${#dates[@]}-1]}" 8 | fi 9 | 10 | stripDate='s/^((.*)Copyright\b(.*?))((?:,\s*)?(([0-9]{4}\s*-\s*[0-9]{4})|(([0-9]{4},\s*)*[0-9]{4})))(?:,)?\s*(.*)\n$/$1$9\n/g' 11 | addDate='s/^.*Copyright(?:\s*\(c\))? /Copyright \(c\) '$datestr' /g' 12 | for l in $dir/LICENSE*; do 13 | perl -pi -e "$stripDate" $l 14 | perl -pi -e "$addDate" $l 15 | done 16 | -------------------------------------------------------------------------------- /.github/workflows/isaacs-makework.yml: -------------------------------------------------------------------------------- 1 | name: "various tidying up tasks to silence nagging" 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | workflow_dispatch: 8 | 9 | jobs: 10 | makework: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | with: 15 | fetch-depth: 0 16 | - name: Use Node.js 17 | uses: actions/setup-node@v2.1.4 18 | with: 19 | node-version: 16.x 20 | - name: put repo in package.json 21 | run: node .github/workflows/package-json-repo.js 22 | - name: check in package.json if modified 23 | run: | 24 | bash -x .github/workflows/commit-if-modified.sh \ 25 | "package-json-repo-bot@example.com" \ 26 | "package.json Repo Bot" \ 27 | "chore: add repo to package.json" \ 28 | package.json package-lock.json 29 | - name: put all dates in license copyright line 30 | run: bash .github/workflows/copyright-year.sh 31 | - name: check in licenses if modified 32 | run: | 33 | bash .github/workflows/commit-if-modified.sh \ 34 | "license-year-bot@example.com" \ 35 | "License Year Bot" \ 36 | "chore: add copyright year to license" \ 37 | LICENSE* 38 | -------------------------------------------------------------------------------- /.github/workflows/package-json-repo.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const pf = require.resolve(`${process.cwd()}/package.json`) 4 | const pj = require(pf) 5 | 6 | if (!pj.repository && process.env.GITHUB_REPOSITORY) { 7 | const fs = require('fs') 8 | const server = process.env.GITHUB_SERVER_URL || 'https://github.com' 9 | const repo = `${server}/${process.env.GITHUB_REPOSITORY}` 10 | pj.repository = repo 11 | const json = fs.readFileSync(pf, 'utf8') 12 | const match = json.match(/^\s*\{[\r\n]+([ \t]*)"/) 13 | const indent = match[1] 14 | const output = JSON.stringify(pj, null, indent || 2) + '\n' 15 | fs.writeFileSync(pf, output) 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.nyc_output 2 | /coverage 3 | /node_modules 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '0.10' 5 | - '4' 6 | - '5' 7 | - '6' 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Please consider signing [the neveragain.tech pledge](http://neveragain.tech/) 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The ISC License 2 | 3 | Copyright (c) 2011-2023 Isaac Z. Schlueter 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 11 | FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 12 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 14 | OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 | PERFORMANCE OF THIS SOFTWARE. 16 | 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Browser-friendly inheritance fully compatible with standard node.js 2 | [inherits](https://nodejs.org/api/util.html#utilinheritsconstructor-superconstructor). 3 | 4 | This package exports standard `inherits` from node.js `util` module in 5 | node environment, but also provides alternative browser-friendly 6 | implementation through [browser 7 | field](https://gist.github.com/defunctzombie/4339901). Alternative 8 | implementation is a literal copy of standard one located in standalone 9 | module to avoid requiring of `util`. It also has a shim for old 10 | browsers with no `Object.create` support. 11 | 12 | While keeping you sure you are using standard `inherits` 13 | implementation in node.js environment, it allows bundlers such as 14 | [browserify](https://github.com/substack/node-browserify) to not 15 | include full `util` package to your client code if all you need is 16 | just `inherits` function. It worth, because browser shim for `util` 17 | package is large and `inherits` is often the single function you need 18 | from it. 19 | 20 | It's recommended to use this package instead of 21 | `require('util').inherits` for any code that has chances to be used 22 | not only in node.js but in browser too. 23 | 24 | ## usage 25 | 26 | ```js 27 | var inherits = require('inherits'); 28 | // then use exactly as the standard one 29 | ``` 30 | 31 | ## note on version ~1.0 32 | 33 | Version ~1.0 had completely different motivation and is not compatible 34 | neither with 2.0 nor with standard node.js `inherits`. 35 | 36 | If you are using version ~1.0 and planning to switch to ~2.0, be 37 | careful: 38 | 39 | * new version uses `super_` instead of `super` for referencing 40 | superclass 41 | * new version overwrites current prototype while old one preserves any 42 | existing fields on it 43 | -------------------------------------------------------------------------------- /inherits.js: -------------------------------------------------------------------------------- 1 | try { 2 | var util = require('util'); 3 | /* istanbul ignore next */ 4 | if (typeof util.inherits !== 'function') throw ''; 5 | module.exports = util.inherits; 6 | } catch (e) { 7 | /* istanbul ignore next */ 8 | module.exports = require('./inherits_browser.js'); 9 | } 10 | -------------------------------------------------------------------------------- /inherits_browser.js: -------------------------------------------------------------------------------- 1 | /* istanbul ignore else - coverage doesn't work without Object.create */ 2 | if (typeof Object.create === 'function') { 3 | // implementation from standard node.js 'util' module 4 | module.exports = function inherits(ctor, superCtor) { 5 | if (superCtor) { 6 | ctor.super_ = superCtor 7 | ctor.prototype = Object.create(superCtor.prototype, { 8 | constructor: { 9 | value: ctor, 10 | enumerable: false, 11 | writable: true, 12 | configurable: true 13 | } 14 | }) 15 | } 16 | }; 17 | } else { 18 | // old school shim for old browsers 19 | module.exports = function inherits(ctor, superCtor) { 20 | if (superCtor) { 21 | ctor.super_ = superCtor 22 | var TempCtor = function () {} 23 | TempCtor.prototype = superCtor.prototype 24 | ctor.prototype = new TempCtor() 25 | ctor.prototype.constructor = ctor 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inherits", 3 | "description": "Browser-friendly inheritance fully compatible with standard node.js inherits()", 4 | "version": "2.0.4", 5 | "keywords": [ 6 | "inheritance", 7 | "class", 8 | "klass", 9 | "oop", 10 | "object-oriented", 11 | "inherits", 12 | "browser", 13 | "browserify" 14 | ], 15 | "main": "./inherits.js", 16 | "browser": "./inherits_browser.js", 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/isaacs/inherits.git" 20 | }, 21 | "author": "Isaac Z. Schlueter (http://blog.izs.me/)", 22 | "license": "ISC", 23 | "bugs": { 24 | "url": "https://github.com/isaacs/inherits/issues" 25 | }, 26 | "homepage": "https://github.com/isaacs/inherits#readme", 27 | "scripts": { 28 | "test": "tap" 29 | }, 30 | "devDependencies": { 31 | "tap": "^15.1.6" 32 | }, 33 | "files": [ 34 | "inherits.js", 35 | "inherits_browser.js" 36 | ] 37 | } 38 | -------------------------------------------------------------------------------- /test/browser.js: -------------------------------------------------------------------------------- 1 | var inherits = require('../inherits_browser.js') 2 | var t = require('tap') 3 | 4 | function test(c) { 5 | t.equal(c.constructor, Child) 6 | t.equal(c.constructor.super_, Parent) 7 | t.equal(Object.getPrototypeOf(c), Child.prototype) 8 | t.equal(Object.getPrototypeOf(Object.getPrototypeOf(c)), Parent.prototype) 9 | t.type(c, Child) 10 | t.type(c, Parent) 11 | } 12 | 13 | function Child() { 14 | Parent.call(this) 15 | test(this) 16 | } 17 | 18 | function Parent() {} 19 | 20 | inherits(Child, Parent) 21 | 22 | var c = new Child 23 | test(c) 24 | 25 | t.type(inherits, 'function') 26 | 27 | function Orphan() {} 28 | inherits(Orphan, null) 29 | -------------------------------------------------------------------------------- /test/builtin.js: -------------------------------------------------------------------------------- 1 | var inherits = require('../inherits.js') 2 | var t = require('tap') 3 | 4 | function test(c) { 5 | t.equal(c.constructor, Child) 6 | t.equal(c.constructor.super_, Parent) 7 | t.equal(Object.getPrototypeOf(c), Child.prototype) 8 | t.equal(Object.getPrototypeOf(Object.getPrototypeOf(c)), Parent.prototype) 9 | t.type(c, Child) 10 | t.type(c, Parent) 11 | } 12 | 13 | function Child() { 14 | Parent.call(this) 15 | test(this) 16 | } 17 | 18 | function Parent() {} 19 | 20 | inherits(Child, Parent) 21 | 22 | var c = new Child 23 | test(c) 24 | 25 | t.type(inherits, 'function') 26 | -------------------------------------------------------------------------------- /test/old.js: -------------------------------------------------------------------------------- 1 | var t = require('tap') 2 | Object.create = null 3 | var inherits = require('../inherits_browser.js') 4 | 5 | function test(c) { 6 | t.equal(c.constructor, Child) 7 | t.equal(c.constructor.super_, Parent) 8 | t.equal(Object.getPrototypeOf(c), Child.prototype) 9 | t.equal(Object.getPrototypeOf(Object.getPrototypeOf(c)), Parent.prototype) 10 | t.type(c, Child) 11 | t.type(c, Parent) 12 | } 13 | 14 | function Child() { 15 | Parent.call(this) 16 | test(this) 17 | } 18 | 19 | function Parent() {} 20 | 21 | inherits(Child, Parent) 22 | 23 | var c = new Child 24 | test(c) 25 | 26 | t.type(inherits, 'function') 27 | 28 | function Orphan() {} 29 | inherits(Orphan, null) 30 | --------------------------------------------------------------------------------