-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
instanceof #34
Comments
Nice to hear you like typify! The
I'll fix |
That Bacon workaround is pretty interesting though I don't find it to be ideal. Thanks for fixing the check for Date and RegExp but the fixes are seemingly not yet reflected in typify.standalone.js. Since, like you said, I was recently made aware that the |
The two below are equivalent: typify.instance(name, cls);
typify.type(name, function (val) {
return val instanceof cls;
}); So the responsibility of using You might exploit var typify = require("./lib/typify.js");
function make(n) {
return function foo() {};
}
var foo1 = make(1);
var foo2 = make(2);
typify.instance("foo1", foo1);
typify.instance("foo2", foo2);
typify.type("foo1x", function (val) {
return val.constructor.name === foo1.name;
});
typify.type("foo2x", function (val) {
return val.constructor.name === foo2.name;
});
var x = new foo1();
var y = new foo2();
console.log(typify.check("foo1", x)); // true
console.log(typify.check("foo2", x)); // false
console.log(typify.check("foo1", y)); // false
console.log(typify.check("foo2", y)); // true
console.log(typify.check("foo1x", x)); // true
console.log(typify.check("foo2x", x)); // true
console.log(typify.check("foo1x", y)); // true
console.log(typify.check("foo2x", y)); // true
Though I could make a standalone build without |
First, thanks for the explanations and updating typify.standalone.js. I prefer strictness but the case of In any case I don't know if such an observation would be useful but I find it interesting nonetheless. |
I'm not sure what is the right term for this behaviour. I wanted to show with this example that: If you have multiple frames communicating data to each other, than there aren't fail-proof method to ensure in the first frame that Yet I don't see this as a problem. You should communicate only raw data (plain objects, arrays, maybe Date's and RegExps which should work now), not "intelligent" objects (as they have references to constructing frame). And validating well-formedness of raw data is what typify is good at. |
Yes, fair point. If someone is trying to be too clever with their data their effort could backfire on them. |
I've been on a quest to find an ideal runtime type-checking library and yours is the most agreeable I've come across thus far.
I notice that you use
instanceof
quite a few times throughout typify.You noted a weakness of
instanceof
which I was unaware of.Unfortunately, there is another weakness
instanceof
has. When used in a browser environment it will not work correctly when dealing with multiple frames in a window. The MDN docs take note of this. This has been discussed elsewhere as well.Of the solutions to this I've seen, the one used by Mithril is pretty nice. What it does is similar to the following:
Note that
{}.toString
is the same asObject.prototype.toString
which I notice you've used a few times.The text was updated successfully, but these errors were encountered: