GithubHelp home page GithubHelp logo

kaelzhang / graceful-instanceof Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 6 KB

The instanceof mechanism cross package/module versions.

License: Other

JavaScript 100.00%
instanceof nodejs class graceful

graceful-instanceof's Introduction

Build Status Coverage

graceful-instanceof

The instanceof mechanism cross package versions.

Why?

export default class MyClass {
  constructor (options) {
    if (this instanceof MyClass) {
      return options
    }

    // do something with options
  }
}

We intend to do something like this:

const instance = new MyClass(options)

instance === new MyClass(instance)  // true

But what happens if the instance is came from another version of the module?

abc.js
node_modules
  |-- foo # version 1.0.0
        |-- index.js # which export default MyClass
  |-- bar
        |-- node_modules # version 1.1.0
        |     |-- foo
        |           |-- index.js # also exports MyClass
        |-- index.js # which exports default the instance of MyClass

And in abc.js

import bar from 'bar'
import MyClass from 'foo'

bar === new MyClass(bar)  // FALSE!!

// Something BOOOOOOOOOOM !!!

Install

$ npm install graceful-instanceof

Usage

import instanceOf from 'graceful-instanceof'

const type = instanceOf('foo:MyClass')

class MyClass {
  constructor (options) {
    if (type.is(options)) {
      return options
    }

    type.attach(this)
  }
}

const instace = new MyClass(options)

instance === new MyClass(instance)  // true

And it also works cross versions.

License

MIT

graceful-instanceof's People

Contributors

kaelzhang avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.