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

can.isArray behaves differently using can.util.jquery vs can.util.domless #1938

Closed
shcarrico opened this Issue Sep 18, 2015 · 2 comments

Comments

Projects
None yet
4 participants
@shcarrico
Contributor

shcarrico commented Sep 18, 2015

jQuery implements the following for isArray checks

Array.isArray

(delegates to native static method)

can.util.domless uses

can.isArray = function (arr) {
        return arr instanceof Array;
    }

This causes odd behavior, in that checks against a class that extends Array return false for the first case and true for the second. IE

 can.isArray = function (arr) {
        return arr instanceof Array;
 };
class MyArray extends Array {}
var a = new MyArray();
var b = [];

//using the above, assuming can.isArray was from can.util.domless

can.isArray(a) //true
can.isArray(b) //true

$.isArray(a) //false
$.isArray(b) //true

codepen

I ran into this because I was making fancy extended arrays, adding tree utility methods etc..

Is this an issue for the libs that use can.util.domless? It seems that one could have a strange bug between a browser usecase and a domless usecase..

@justinbmeyer

This comment has been minimized.

Show comment
Hide comment
@justinbmeyer

justinbmeyer Sep 18, 2015

Contributor

@shcarrico DOM-less should probably be using the ol' Object.prototype.toString.call(arr) === "[object Array]" trick if Array.isArray doesn't exist.

Contributor

justinbmeyer commented Sep 18, 2015

@shcarrico DOM-less should probably be using the ol' Object.prototype.toString.call(arr) === "[object Array]" trick if Array.isArray doesn't exist.

@rjgotten

This comment has been minimized.

Show comment
Hide comment
@rjgotten

rjgotten Sep 18, 2015

@justinbmeyer
It should absolutely be using the prototype.toString method of determining whether a value is an array. The instanceof Array method fails where array data is communicated across window boundaries. (One window's Array is not another window's Array.)

rjgotten commented Sep 18, 2015

@justinbmeyer
It should absolutely be using the prototype.toString method of determining whether a value is an array. The instanceof Array method fails where array data is communicated across window boundaries. (One window's Array is not another window's Array.)

mjstahl added a commit that referenced this issue Sep 25, 2015

@daffl daffl added this to the 2.3.0 milestone Sep 28, 2015

mjstahl added a commit that referenced this issue Oct 22, 2015

daffl added a commit that referenced this issue Oct 22, 2015

Merge pull request #1954 from bitovi/1938-better-is-array
Fixes #1938. Check for isArray, if not use toString method

@mjstahl mjstahl closed this in d0f20bf Oct 22, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment