freed script with hasOwnProperty

2019-08-06 23:29发布

I have a page containing an iframe in which I expect that within the iframe the user will navigate to many different pages - all of them in the same domain as the parent page.

In the top level window I have a persistent object, let's call it appData. And in the iframe I often have lines like

parent.appData[someProperty] = {a : 1, b : 2};

I'm aware of the general problem of freed script errors and I understand that I shouldn't try to call any arbitrary methods on such an object since the originating document may have unloaded, but surely hasOwnProperty shouldn't be such a method, and I should be allowed to say, in some subsequent child page:

if (parent.appData[someProperty].hasOwnProperty('a'))

Shouldn't I? And here's what's odd: it works as I expect in every browser I've tested except MSIE 10. I did see the recent answer to IE9 "Can't execute code from freed script" when calling hasOwnProperty() and indeed using in seems to work fine for my case, but I'm wondering whether I've been "cheating" all along or if this is a bug in MSIE 10.

jsFiddle doesn't handle iframes so I'm not sure how best to demonstrate this with an example, sorry.

1条回答
甜甜的少女心
2楼-- · 2019-08-07 00:06

Personally, I'd handle all functions similarly if you are concerned about the freed script issue, including built-in ones such as hasOwnProperty. In that specific case, it still could technically be interpreted as a function on an object, and apparently some browsers treat it as such (IE10) and others treat it as a special case that is still accessible (the others you mention). Even if it worked in all browsers, I'd still feel wrong about it.

Thus, not sure about your exact case, but could you instead do something like:

var appData = parent.appData || {};
var someProperty = appData[someProperty] || {};
if(someProperty[a] !== undefined){
    ..
}

Otherwise, you could always surround it with a try..catch

Update
And of course you could use in as well, as mentioned in your linked question.

查看更多
登录 后发表回答