Giter Site home page Giter Site logo

yangxin1994 / clone-class Goto Github PK

View Code? Open in Web Editor NEW

This project forked from huan/clone-class

0.0 0.0 0.0 98 KB

Clone an ES6 Class as Another Class Name for Isolating Class Static Properties.

Home Page: https://npmjs.com/package/clone-class

License: Apache License 2.0

TypeScript 78.99% Shell 21.01%

clone-class's Introduction

CLONE CLASS

NPM Version Build Status TypeScript

Clone Class

Clone an ES6 Class as Another Class Name for Isolating Class Static Properties.

EXAMPLE

Run the following example by:

$ git clone [email protected]:zixia/clone-class.git
$ cd clone-class
$ npm install
$ npm run example

TypeScript

Example Source Code:

import * as assert from 'assert'

import {
  cloneClass,
  instanceToClass,
}                   from '../src/clone-class'

class Employee {
  public static company: string

  constructor(
    public name: string,
  ) {
  }

  public info() {
    console.log(`Employee ${this.name}, Company ${(this.constructor as any).company}`)
  }
}

/**
 * Example 1: `cloneClass()`
 */
const GoogleEmployee = cloneClass(Employee)
GoogleEmployee.company = 'Google'

const MicrosoftEmployee = cloneClass(Employee)
MicrosoftEmployee.company = 'Microsoft'

const employeeGg = new GoogleEmployee('Tom')
const employeeMs = new MicrosoftEmployee('Jerry')

employeeGg.info()
// Output: Employee Tom, Company Google
employeeMs.info()
// Output: Employee Jerry, Company Microsoft

/**
 * Example 2: `instanceToClass()`
 */
const RestoreGoogleEmployee = instanceToClass(employeeGg, Employee)
assert(RestoreGoogleEmployee === GoogleEmployee, 'Should get back the Class which instanciated the instance)
assert(RestoreGoogleEmployee !== Employee, 'Should be different with the parent Class')

const anotherEmployee = new RestoreGoogleEmployee('Mary')
anotherEmployee.info()
// Output: Employee Mary, Company Google

The most tricky part of this code is (this.constructor as any).company.

It will be very clear after we break down it as the following steps:

  1. this.constructor is the constructor function of the class, which shuold be the class function itself.
  2. company is a static properity defined in Employee class, which will be set as a property on the class function.
  3. So this.constructor.company is equal to Employee.company, except that we will not need to know the exact name of the class, Employee in this case. We use this pattern is because we need to visit the class function even we do not know it's name.

API

We have two APIs for dealing with the classes:

  1. cloneClass(OriginalClass): create a new Class that is extend from the OriginalClass which can isolate static properties for stored values, and return the new Class.
  2. instanceToClass(instance, BaseClass): get the Class which had instanciated the instance, which is the BaseClass, or the child class of BaseClass, and return it.

cloneClass()

const AnotherClass = cloneClass(OrignalClass)
const instance = new AnotherClass()

instanceToClass()

const RestoredClass = instanceToClass(instance, OrignalClass)
assert(RestoredClass === AnotherClass, 'because `instance` was created by `new AnotherClass()`')

Constructor<T>

const NewableClass: typeof AbstractClass & Constructor<AbstractClass>
const instance = new NewableClass()

It seems useless at first, but if you want to use manage many Child Class for a Abstract Class with typings, then it will be a must have tool.

CHANGELOG

v0.6 (May 2018)

  1. add new function: instanceToClass() for getting back the Class from an existing instance.
  2. add new type: Constructor<T> for adding new (): T to abstract class declaration.

v0.4 (Apr 2018)

First publish version.

  1. cloneClass() work as expected.

v0.0.1 (Apr 23, 2018)

Initial version, code comes from Project Wechaty.

Learn more about the full story at Chatie blog: New Feature: Multi-Instance Support for Wechaty v0.16(WIP)

SEE ALSO

An UseCase of clone-class can be found in an article writen by me from Chatie blog, it's also the place where this module comes from. It's Worth to spent some time to have a look if you are interested.

AUTHOR

Huan LI <[email protected]>

profile for zixia on Stack Exchange, a network of free, community-driven Q&A sites

COPYRIGHT & LICENSE

  • Code & Docs © 2018 Huan LI <[email protected]>
  • Code released under the Apache-2.0 License
  • Docs released under Creative Commons

clone-class's People

Contributors

dependabot-preview[bot] avatar greenkeeper[bot] avatar huan 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.