Message from JavaScript discussions

September 2017

— "If it looks like a duck and quacks like a duck, it's a duck"


In JS this is commonly used to validate incoming parameter objects. I wrote something that does it:

assert.props(obj, "prop" ...);

— Very handy :D

— Non-ducktyping version of this would be: e.g. typeof parameter == 'whatever'

— Yeah

— But that's kind of bad practice in languages with duck-typing, since it means you can't easily subclass/add behaviour

Message permanent page

— Or instanceof or isPrototypeOf

— Complex duck typing can also validate functions, I think

— But that's more unit-testy and less production

— Well, instanceof would still work in JS:

class MyError extends SyntaxError {
new TypeError() instanceof Error // true
new MyError() instanceof Error // true

Message permanent page

— I rarely use classes

— I'm getting buffer.alloc is not a function error when I try to install using npm