auto-object
Create an object or a class that may accessed by any property name just like Proxy.
eg.
const myObject = autoObject; myObjectfoo; ///< "foo"myObject; ///< "Foo"myObject; ///< "Bar"
Installation
$ npm install --save auto-object
API
Require auto-object
at first:
const autoObject = ;
createClass
Create an auto-object class and get its constructor function:
autoObject
className
: the class name, default toAutoClass
. (optional)- return: the special constructor function
Your own constructor function should attached on the prototype chain named
$$constructor
. And the access function should attached on the prototype chain
named $$access
. Both of $$constructor
and $$access
are optional.
$$constructor
What's We usually create a class constructor function like this:
{ this$val = val;}
But createClass
exactly returns a constructor function which you can't
redefine. So we make $$constructor
as your own constructor function.
eg.
const MyClass = autoObject; MyClassprototype { this$$val = val;}; const ret = 233; ///< MyClass { $$val: 233 }
$$access
What's $$access
is the access function to deal with property accessing. This function
owns a single parameter which means the property name. You should return a
result for the accessing.
eg.
const MyClass = autoObject; MyClassprototype { this$$val = val;}; MyClassprototype { const self = this; ifproperty return { self$$val += ; return $$val; }; else ifproperty return { self$$val -= ; return $$val; }; return "You're hacked";}; const ret = 233; ///< MyClass { $$val: 233 }ret; ///< 238ret; ///< 240ret; ///< 230retfoo; ///< "You're hacked"
Inherits
The result of createClass
almost like the normal constructor function, it may
inherit from some other class by using util.inherits
too.
eg.
const MyClass = autoObject; MyClassprototype { EventEmitter;}; util; MyClassprototype { const self = this; return { self; };} const ret = ;ret;ret; // emit("test", "Hello world")
createObject
Create and get an auto-object:
autoObject;
access
: the access function. (optional)- return: the special auto-object
If you want to change access function lately, you just assign the function to
the $$access
property of the returned object.
$$access
What's $$access
is the access function to deal with property accessing. This function
owns a single parameter which means the property name. You should return a
result for the accessing.
eg.
const obj = autoObject; objfoo; ///< "You're foo"objbar; ///< "You're bar"
You can change $$access
at any time you want to do this.
eg.
const obj = autoObject; objfoo; ///< undefinedobj { return `You're `;};objfoo; ///< "You're foo"objbar; ///< "You're bar"
Reserved Properties and Overwrite Rule
This is important!
Here's a reserved properties list that won't go through the access function:
"constructor" "hasOwnProperty" "isPrototypeOf" "propertyIsEnumerable" "toLocaleString" "toString" "valueOf" "__defineGetter__" "__defineSetter__" "__lookupGetter__" "__lookupSetter__" "__proto__" "inspect" "$$constructor" "$$access"
And if you define a property manually in your auto-object, that certain property won't go through the access function too.
eg.
const obj = autoObject; objfoo; ///< "You're hacked"objfoo = "I'm free";objfoo; ///< "I'm free"delete objfoo;objfoo; ///< "You're hacked"
util.inherits
*
Hacking If you're using Node.js v4.x or you're considering to backward compatible v4.x
in your program, you should not use util.inherits
to inherit your Auto-Object
class.
As a substitute, you should use autoObject.inherits
instead. This function is
same as util.inherits
when you're using v6.x or higher. But if you're using
v4.x, it's a hacking.
The code is like this under v4.x:
forconst key in superCtorprototype ctorprototypekey = superCtorprototypekey;
The behavior is not 100% same as util.inherits
. So in fact I don't recommend
you use this package under v4.x.
Contribution
You're welcome to fork and make pull requests!
「雖然我覺得不怎麼可能有人會關注我」