项目作者: aimingoo

项目描述 :
Metameta is meta core and meta-class programming framework.
高级语言: JavaScript
项目地址: git://github.com/aimingoo/metameta.git
创建时间: 2018-08-17T07:46:17Z
项目社区:https://github.com/aimingoo/metameta

开源协议:Apache License 2.0

下载


Metameta

Metameta is meta core and meta-class programming framework.

Usage

  1. # install from npm
  2. > npm install @aimingoo/metameta
  3. # OR, install from github
  4. > npm install aimingoo/metameta

And use in javascript

  1. // Meta core
  2. var {Meta, MetaClass, MetaObject} = require('@aimingoo/metameta');
  3. // Helper methods, all home object safed.
  4. var {isMeta, isAtom, asAtom} = Meta;
  5. // learn case
  6. var Objext = Meta.from(Object);
  7. console.log(Objext.keys(new Objext)); // []
  8. console.log(Meta.isAtom(new Objext)); // true

The Meta

atom

The atom is smallest unit of pure object in javascript, it’s object but not inherited from Object(). An atom is a object. null, Object.prototype, arguments and namespace are atom objects in javascript language.

  1. > Meta.isAtom(null);
  2. true
  3. > Meta.isAtom(Object.prototype)
  4. true

meta/Atom

atom created by meta, so the meta is constructor of atoms, with Atom() on the concept is the same. meta is base unit of Meta system, and is function always.

  1. # meta is same of Atom()
  2. > meta = new Meta;
  3. # atom created
  4. > atom = new meta;
  5. # check is atom
  6. > Meta.isAtom(atom);
  7. true
  8. # check is object
  9. > typeof atom;
  10. 'object'

Meta

Meta is meta’s class.

  1. // extend
  2. class MetaX extends Meta {};
  3. var meta = new MetaX;
  4. // meta is atom too.
  5. console.log(Meta.isAtom(new Meta)); // true
  6. console.log(Meta.isAtom(meta)); // true
  7. // instance from meta, check inheritance
  8. var x = new meta;
  9. console.log(x instanceof meta); // true
  10. console.log(x instanceof Meta); // true
  11. console.log(x instanceof MetaX); // true
  12. // another
  13. class MetaX2 extends Meta {};
  14. console.log(x instanceof MetaX2); // false

MetaClass

MetaClass is root of Meta-classes programming system.

  1. // meta-classes
  2. class MetaClassEx extends MetaClass {
  3. // ...
  4. }
  5. // extend meta system
  6. // NOTE: must extends from a meta/Atom
  7. class AtomObjects extends new MetaClassEx() {
  8. // ...
  9. }
  10. // check
  11. console.log(MetaClass.isClassOf(MetaClassEx)); // true
  12. console.log(MetaClassEx.isClassOf(AtomObjects)); // true
  13. console.log(MetaClassEx.isClassOf(new AtomObjects)); // true
  14. // another
  15. console.log(MetaClassEx.isClassOf(new MetaObject)); // false

MetaObject

MetaObject is a meta of MetaClass, and is a Class/Atom-constructor of atoms.

  1. // MetaObject is defined in metameta
  2. var obj = new MetaObject;
  3. console.log(Meta.isAtom(obj)); // true
  4. // extend object system
  5. class MetaObjectEx extends MetaObject{}
  6. // create atom
  7. var x = new MetaObjectEx;
  8. // check
  9. console.log(MetaClass.isClassOf(MetaObject)); // true
  10. console.log(MetaClass.isClassOf(MetaObjectEx)); // true
  11. console.log(MetaClass.isClassOf(x)); // true
  12. // check in pure javascript
  13. console.log(x instanceof MetaObject); // true
  14. console.log(x instanceof MetaObjectEx); // true
  15. console.log(x instanceof MetaObject); // true
  16. console.log(x instanceof MetaClass); // true
  17. console.log(x instanceof Meta); // true
  18. // more
  19. var isPrototypeOf = (...args) => Object.prototype.isPrototypeOf.call(...args);
  20. console.log(isPrototypeOf(MetaObject, MetaObjectEx)); // true
  21. console.log(isPrototypeOf(MetaClass, MetaObject)); // false
  22. console.log(isPrototypeOf(MetaClass, MetaObjectEx)); // false

Shadow meta

A shadow is meta from native constructor.

  1. // shadow from native-constructor
  2. var Objext = Meta.from(Object);
  3. // shadow is meta
  4. var x = new Objext;
  5. console.log(Meta.isAtom(x)); // true
  6. // shadow has static methods from source
  7. console.log(Objext.keys(x)); // []

More testcases

  1. # pull source
  2. > git clone https://github.com/aimingoo/metameta
  3. > cd metameta
  4. > npm install
  5. # test
  6. > npm test
  7. # coverage
  8. > npm -g install istanbul
  9. > npm run coverage

Interface

  • Meta’s Class methods

    1. // Return a object is atom object
    2. // @param {object|function} - a object
    3. Meta.isAtom(x)
    4. // Return a new atom/Atom, it's proxy of x, and prototype by base.
    5. // @param {object}[object] - a atom base another atom object
    6. // @param {function}[function=Atom] - a meta base meta/Meta/Atom...
    7. Meta.asAtom(x, base)
    8. // Return a class is Meta
    9. // @param {function} - a class
    10. Meta.isMeta(cls)
    11. // Return a shadow meta for native-constructor
    12. // @param {function} - a native constructor
    13. Meta.from(constructor)
  • MetaClass’s Class methods

    1. // Return the class is base of x
    2. // @param {object} - is instance of class
    3. // @param {function} - is sub-class of class
    4. MetaClass.isClassOf(x)
  • extends from Meta

    1. class MetaX extends Meta {}
  • meta’s extends

    1. // Meta-classes programming framework based
    2. class X extends new MetaClass {}
    3. // OR
    4. class X extends new MetaX{}

History

  1. 2018.08.17 - v0.9.1 released, happy Chinese traditional valentine's day.