Skip to content
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

h$isObject is implemented incorrectly in jsbits/utils.js. #68

Open
crocket opened this issue Oct 8, 2016 · 4 comments
Open

h$isObject is implemented incorrectly in jsbits/utils.js. #68

crocket opened this issue Oct 8, 2016 · 4 comments

Comments

@crocket
Copy link
Contributor

crocket commented Oct 8, 2016

Let's have a look at

function h$isObject(o) {
return typeof(o) === 'object';
}

function h$isObject(o) {
    return typeof(o) === 'object';
}

typeof(null) is 'object', thus you should replace that with something like

function h$isObject(o) {
    return (o instanceof Object);
}

I don't know if instanceof Object is totally accurate yet. I'll add comments on this.

@crocket
Copy link
Contributor Author

crocket commented Oct 27, 2016

According to http://stackoverflow.com/a/8511350/2104107 and http://stackoverflow.com/a/22482737/2104107,

function h$isObject(o) {
    return o !== null && typeof o === 'object';
}

seems correct. Since functions are considered objects in javascript specification, the following function could be considered correct, too.

function h$isObject(o) {
    return o !== null && (typeof o === 'object' || typeof o === 'function');
}

@hamishmack
Copy link
Member

Is there an example use case where the current implementation is problematic? It seems like it would be best to mirror the behaviour of JavaScript in this case unless there is a reason not to.

@crocket
Copy link
Contributor Author

crocket commented Jul 7, 2017

I stopped programming for money. I'm transitioning into art. Nowadays, I rarely ever write code except when I write some haskell code in XMonad and small command line programs. Thus, I also stopped caring about user interface, javascript, and GHCJS.

That said, I don't know what you mean by mirroring the behavior of javascript.
Does javascript have isObject?
Anyway, I defer this issue entirely to you or anyone else.

@hamishmack
Copy link
Member

I stopped programming for money.

Ah.

I'm transitioning into art.

Nice.

Nowadays, I rarely ever write code except when I write some haskell code in XMonad and small command line programs. Thus, I also stopped caring about user interface, javascript, and GHCJS.

Doh!

That said, I don't know what you mean by mirroring the behavior of javascript.
Does javascript have isObject?

Good point. I guess I just was following the pattern that the other isX options were all the typeof of the value. We already have a wrapper for typeof so I think you are right it would be nicer for isObject to be a bit more OO in there behaviour (exclude null and include functions).

Also reading through that post in more detail suggests o === Object(o) works the same as (typeof(o) === 'object' \|\| typeof(o) === 'function') && o !== null. I think that might be the way to go because it could even be inlined inexpensively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants