incubator-callback-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian LeRoux...@brian.io>
Subject Re: `instanceof` considered harmful in cordova-js
Date Fri, 27 Apr 2012 18:38:47 GMT
Would like to know the exact offending piece of code too. I'd question
the need of even having that check.... its good to be defensive but
this code would just silently return instead of failing noisily
anyhow.

On Fri, Apr 27, 2012 at 11:29 AM, Jesse <purplecabbage@gmail.com> wrote:
> Channel.prototype.subscribe = function(f, c, g) {
>    // need a function to call
>    if (f === null || f === undefined) { return; }
>
>    var func = f;
>    if (typeof c == "object" && f instanceof Function) { func =
> utils.close(c, f); }
>
>
> I assume we are talking about the above in our code.
>
>
> Can we get away with simply doing this? :
>
> Channel.prototype.subscribe = function(f, c, g) {
>    // need a function to call
>    if ( !f || !f.apply ) { return; }
>
>    var func = f;
>    if (typeof c == "object") { func = utils.close(c, f); }
>
>
>
>
> On Fri, Apr 27, 2012 at 11:22 AM, Michael Brooks
> <michael@michaelbrooks.ca>wrote:
>
>> I believe the latest jQuery source has dropped support for a lot of legacy
>> browsers. Regardless, even release 1.6.1 uses the implementation.
>>
>> On Fri, Apr 27, 2012 at 11:17 AM, Filip Maj <fil@adobe.com> wrote:
>>
>> > For reference: https://issues.apache.org/jira/browse/CB-588
>> >
>> > Essentially: using instanceof across windows (basically: iframes) is bad.
>> > Daniel from the issue thread above posted a link [1] explaining the
>> > underlying issue in a bit more detail.
>> >
>> > In this specific example, GWT apps use an iframe to create initialization
>> > callbacks and whatnot. Attaching these callbacks to deviceready (or other
>> > cordova events) in the parent window doesn't work because instanceof
>> > doesn't work across frames.
>> >
>> > The link [1] offers a workaround: calling toString() on whatever you want
>> > to check the instance of and comparing it against "[object
>> > myPrototypeName]". My initial thought is, that seems weak, but taking a
>> > look at jQuery source (see [2] and [3] together), they use this same
>> > approach. That tells me it's pretty battle hardened.
>> >
>> > I'm leaning towards employing the toString() approach instead of a
>> > combination of typeof and duck-typing to fix this issue.
>> >
>> > I wanted to run it by the list to make sure this sounds alright and see
>> if
>> > anyone has any problems with this.
>> >
>> > Cheers,
>> > Fil
>> >
>> > [1]
>> >
>> http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-
>> > robust-isarray/
>> > [2] https://github.com/jquery/jquery/blob/master/src/core.js#L471-L491
>> > [3] https://github.com/jquery/jquery/blob/master/src/core.js#L900-L902
>> >
>> >
>>

Mime
View raw message