When I send a JS object to a Node.js server and ask for the same object back, the server sends back a modified object

I have a web page that establishes communication with a node.js server. I am sending it objects and having it return the same object that it received. However, the object that is returned is different - all of its methods are gone.

Here's code that shows what I'm doing. I send an event with an object:

editor.getSession().on('change', function(event) {
    socket.emit('modificarCodigo', event);
});

And I return it like this:

socket.on('modificarCodigo', function(data) {
    socket.broadcast.to(socket.grupo).emit('actualizarCodigo', data);
});

How can I get node.js to return the object that I send to it intact instead of modifying it? I have a guess that the behavior I'm seeing now is by design - if that's true, how can I change it?

You cannot serialize functions into JSON. If you really need to send functions then you'll have to .toString() them, send them, and then eval or new Function them back into existence, but that is a risky proposition... Why do you need to send methods?

Edit: The OP added a comment that what he is trying to do is pass and Ace editor Range object over the wire... I think you would be better off sending just the data required to build a new Range object. So just send startRow, startCol, endRow, and endCol and then on the other end of the wire you can do new Range(data.startRow, data.startCol, data.endRow, data.endCol).

As a side note, the proper serialization of an object can be handled (if your js engine supports JSON.stringify/parse) using a toJSON method. Here is an example:

function Dude(name, age) {
    this.name = name;
    this.age = age;
}
Dude.prototype = {
    speak: function() { alert('My name is ' + this.name); },
    toJSON: function() {
        return {
            type: 'Dude',
            props: { name: this.name, age: this.age }
        };
    }
};

http.get('http://getadude.com', function(json) {
    var data = JSON.parse(data);

    if(type === 'Dude') data = new Dude(data.props.name, data.props.age);
})

Serialize your object as JSON before sending it and decode it after. It should work.

Sounds like your really wanting RPC, for which node has an awesome library called dnode. https://github.com/substack/dnode