开发者

Is there a point in compensating for multiple frames/windows nowadays in Javascript?

In Douglas Crockford's book, he states that

if (my_value && typeof my_value === 'object' &&
        typeof my_value.length === 'number' &&
        !(my_value.propertyIsEnumerable('length')) {
    // my_value is truly an array!
}

is the best way to check if a variable is an array, because it compensates for 开发者_如何学Pythonvariables in other frames or windows.

Should one still (in 2011) be coding for and around multiple frames and windows?


Yes. <iframe> elements are still very much part of web development today and for the foreseeable future.

However, if your page or application is guaranteed never to use iframes, you can omit the checks recommended by Crockford, perhaps with comments in the code to draw attention to the fact you're doing so.


iframe elements aren't the only way of getting multiple context scripts. A simple call to window.open will produce a new window context. Popups tend to be less common now-a-days, but they are still readily available in good UI designs.

Take Gmail for instance, not only are iframes used all over the place, but you can also pop out your message and chat windows to have a separate window to move around. If you're creating a plugin that relies on this functionality, you'd be better off sticking to the tried-and-true method.


What book and what print?

I have some experience with the nastiness of arrays that aren't really arrays in JavaScript. Such as: select.options (the options property of a <select> element) and arguments (the magic variable). I don't know if these pass the test you mention but I'm very much afraid they will.

Knowing whether something is an array isn't very useful by itself. You likely want to know if you can iterate over it, or if it has a length property or a slice function for instance.

So I guess it's a bit of a moot point, but I'd be interested to see what anyone else can come up with.

As for frames, as I've outlined, I don't think frames are your biggest concern. I don't really see what frames have to do with arrays, unless they also contain a similar gotcha

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜