$ npm install marked_types
Fix for instanceof issues.
Using npm packaging you can easily get into situation when instanceof
returns false even if object is actually an instance of given type.
This may happen when some module is loaded more than once from several different locations. It is common situation because of the way npm handles dependencies.
Note, that you can run into this problem not only with npm itself, but also by using browserify, working with different execution contexts and possibly in number of other situations.
For more information, see problem details.
Most cases when you want to use instanceof are simple enough:
For both cases it's enough to mark somehow your types of interest and later check objects on having corresponding markers. That's exactly what marked_types allows to do.
Marked types can be used in node.js and also in browser using browserify or something similar.
NOTE Type ids are always compared as strings for type checking purposes, without any parsing or deeper analysis. Marked types supposed to be fast, not smart. The only reason type ids have some specific format is to guarantee global uniqueness.
Type id format:
packageName{URI}(versionSpecifier):idWithinPackage
{URI}
part is optional if package is registered in npm under packageName specified, because this makes packageName unique enough(versionSpecifier)
part is optional and can use whatever agreements you want for your package versioning
idWithinPackage
part is unique type id within your package
(client):MyType
, (server):MyType
lib/client:MyType
, lib/server:MyType
It's recommended to:
Escaping:
Id examples:
myPkg:MyType
myPkg:(server):MyType
myPkg:lib/client:MyType
myPkg{http://myprojectpage.com/myPkg}:(client):MyType
{http://myprojectpage.com/myOtherPkg}:(client):MyType
myPkg(>=0.1.0):MyType
It works well with inherited types as far as they have correct super_
property referencing supertype. To distinguish type from it's marked supertypes, the type itself should be marked too. See "Usage" and "Internals" below for details.
Types marked with the same id are equivalent for marked_types even if they belong to different package versions. You can use versionSpecifier part to prevent this. But also you can use versionSpecifier to state that some type can be freely used across several versions.
For example:
myPkg(0.1.0):MyType
within itmyPkg(>=0.1.1):MyType
myPkg(>=0.2.0):MyType
This case:
Note, that versionSpecifier has no direct relation to your package version. For marked_types it's just part of id and have no special meaning. But you can use it to do tricks described above.
You can mark type when it is declared:
var mt = require('marked_types');
var MyType = function () {
};
mt.mark(MyType, 'myPkg:MyType');
And then check if an object is instance of your type:
var obj1 = new MyType();
mt.is(obj1, MyType); // true
mt.is(obj1, SomeOtherType); // false
This will also work for inherited types:
var inherits = require('util').inherits;
var MyOtherType = function () {
};
inherits(MyOtherType, MyType);
var obj2 = new MyOtherType();
mt.is(obj2, MyType); // true
// but
mt.is(obj2, MyOtherType); // false - because MyOtherType is not marked
If you want to be able to detect inherited type also, you should mark it too:
var MyThirdType = function () {
};
inherits(MyThirdType, MyType);
mt.mark(MyThirdType, 'myPkg:MyThirdType');
var obj3 = new MyThirdType();
mt.is(obj3, MyType); // true
mt.is(obj3, MyThirdType); // true
mt.is(obj3, MyOtherType); // false
mark(type, id)
sets typeMarker_
property of type to id specified.
is(obj, type)
gets marker of type
or it's nearest marked supertype (if type
itself has no marker) and returns true if the marker obtained is the same as marker of obj.constructor
or one of it's supertypes. It uses super_
property to get supertypes.
Note, that internals can be changed in future implementations.
MIT
© 2010 - cnpmjs.org x YWFE | Home | YWFE