Giter Club home page Giter Club logo

Comments (10)

ericelliott avatar ericelliott commented on May 18, 2024 1

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.

koresar avatar koresar commented on May 18, 2024

Here are the changes. Please, take a look at the three new unit tests.
v2_0...instanceof

All the tests pass.

from stampit.

ericelliott avatar ericelliott commented on May 18, 2024

I appreciate the effort and enthusiasm, but I'm not going to encourage users to make mistakes.

from stampit.

koresar avatar koresar commented on May 18, 2024

I see.
Any recommendations on how to ducktype objects made by particular stamp?

from stampit.

ericelliott avatar ericelliott commented on May 18, 2024

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.

danielkcz avatar danielkcz commented on May 18, 2024

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.

koresar avatar koresar commented on May 18, 2024

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.

danielkcz avatar danielkcz commented on May 18, 2024

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.

ericelliott avatar ericelliott commented on May 18, 2024

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.

danielkcz avatar danielkcz commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.