Comments (10)
Don't trust instanceof
in JavaScript. It does not do what the name implies that it does. It does not work across execution contexts. It doesn't work if the fn.prototype
property gets changed after the object was instantiated. Basically, it's a completely broken concept in JS. It doesn't remotely make the guarantees that Java people expect it to make.
Never require new
(or suggest it, or imply that consumers can use it).
http://ericleads.com/2013/01/javascript-constructor-functions-vs-factory-functions/
http://ericleads.com/2012/09/stop-using-constructor-functions-in-javascript/
https://medium.com/javascript-scene/the-two-pillars-of-javascript-ee6f3281e7f3
from stampit.
Here are the changes. Please, take a look at the three new unit tests.
v2_0...instanceof
All the tests pass.
from stampit.
I appreciate the effort and enthusiasm, but I'm not going to encourage users to make mistakes.
from stampit.
I see.
Any recommendations on how to ducktype objects made by particular stamp?
from stampit.
Yeah. Ducktype. Checking instanceof
is the opposite of ducktyping.
Ducktyping means that you check the object instance, and if it has the stuff you expect to have, you just assume it's a compatible type and use it.
from stampit.
Found a reference to this on gitter and I am just curious if duck typing is sort of the same thing as type checking? I mean there are things like flow-runtime which can turn static types to runtime checks, so is there any actual difference?
from stampit.
Looking at the amount of code flow-runtime
has and at the the code example it's probably implemented somehow different to just instanceof
proposed above.
from stampit.
I don't care about instanceof
, I agree with @ericelliott that it's misleading. The flow-runtime
just turns static typings into runtime code.
const MyStamp = methods({
convert(input: string): number {
return Number(input)
}
})
function myFunctionExpectingStamp(obj: { convert: (string) => number }) {
// everything is cool, the `obj.convert` is what we need
}
myFunctionExpectingStamp(MyStamp())
You can try here what is produced: https://codemix.github.io/flow-runtime/#/try
from stampit.
Flow mostly relies on structural type checks, which is like automatic duck typing. I think Flow uses nominal checks in some situations, which is bad and not like duck typing. TypeScript uses all structural checks, AFAIK.
from stampit.
Flow uses structural typing for objects and functions, but nominal typing for classes.
https://flow.org/en/docs/lang/nominal-structural/
Honestly these are new terms for me :)
Anyway in the example above it's clearly a structural check, so it can be considered same thing as duck typing. From my understand the duck typing is something like this, right ?
function myFunctionExpectingStamp(obj) {
if (!(obj && typeof obj.convert === 'function') {
throw new Error('missing convert method');
}
}
Using type checker it looks much more clean, readable, reusable and much more robust in my opinion. So my question could be: "Is duck typing obsolete?"
from stampit.
Related Issues (20)
- Performance degradation in node.js 8 HOT 12
- 'Use metadata in static functions' sample code throws an error HOT 1
- Suggestions on how to leverage stampit w/ Vue.js? HOT 6
- Need some guidance HOT 5
- β¨ Add .version to stampit object HOT 4
- Link to API docs is broken in Readme? HOT 3
- Compose using an instance? HOT 4
- [Bug] RangeError: 'Maximum call stack size exceeded' when use setters HOT 3
- [SOLVED] How to express stampit factories and instances in typescript HOT 4
- Implementing the Decorator Pattern HOT 5
- π Stamps vs Traits vs Mixins vs Has-a/Is-a composition vs Multiple inheritance (classes) feature-by-feature comparison HOT 3
- when i return something in init(), and then i can not call all methods HOT 10
- when i use getter and setter, i got this, is this a issue? HOT 21
- array in props persist across instances of stamp HOT 1
- Website is down HOT 3
- Provide ES6 Module build HOT 8
- Not so much an issue but a question. HOT 4
- Please helpοΌI would like update V2 to V4 HOT 1
- RFC: TypeScript version bump + Type enhancements HOT 4
- Can't seem to mock axios in a stamp HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stampit.