JS: capture a static snapshot of an object at a point in time with a method

Posted by Barney on Stack Overflow See other posts from Stack Overflow or by Barney
Published on 2010-04-13T11:07:44Z Indexed on 2010/04/13 11:13 UTC
Read the original article Hit count: 325

I have a JS object I use to store DOM info for easy reference in an elaborate GUI.

It starts like this:

var dom = {
    m:{
        old:{},

        page:{x:0,y:0},
        view:{x:0,y:0},

        update:function(){
            this.old = this;
            this.page.x = $(window).width();
            this.page.y = $(window).height();
            this.view.x = $(document).width();
            this.view.y = window.innerHeight || $(window).height();
        }

I call the function on window resize:

$(window).resize(function(){dom.m.update()});

The problem is with dom.m.old. I would have thought that by calling it in the dom.m.update() method before the new values for the other properties are assigned, at any point in time dom.m.old would contain a snapshot of the dom.m object as of the last update – but instead, it's always identical to dom.m. I've just got a pointless recursion method.

Why isn't this working? How can I get a static snapshot of the object that won't update without being specifically told to?

Comments explaining how I shouldn't even want to be doing anything remotely like this in the first place are very welcome :)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about javascript-execution