this
основано на том, как вызывается функция.Вызов myparent.getchildvalue()
- это нормально, но как только вы назначите myparent.getchildvalue
в качестве обработчика, он будет вызван вне контекста .Вы можете продемонстрировать это просто:
var obj = { val: 42, fn: function(){ alert(this.val) } };
ref = obj.fn;
alert(obj.fn === ref); // true, so expect the following to do the same thing...
obj.fn(); // 42, so far so good
ref(); // undefined. uh oh...
Вы можете обойти это, обернув:
...
window.onbeforeunload = function(){ myparent.getchildvalue(); };
...
или связав:
...
window.onbeforeunload = myparent.getchildvalue.bind(myparent);
...