Javascript - can't modify/change var object value inside function?
var shared = {
        lock: true,
        connection: '',
        messages: []
    };
Socket.onopen = function(e)
    {
        shared.connection = 'Zostałeś połączony pomyślnie!';
        shared.lock = false;
    };
Something like this, and it doesn't work.
But code is proper, because something like this
shared.connection = 'Zostałeś połączony pomyślnie!';
Socket.onopen = function(e)
        {
            shared.lock = false;
        };
does work.
so It's like I can't modify shared.connection inside this function...
But what's strange
Socket.onerror = function(e) { shared.messages.push(generateMessage('System', 'Wystąpił błąd! Połączenie zostało przerwane.', 'others')); shared.lock = true; };
This one works, pushes properly message to messages array in shared.
What's up? What is going on?
How to get it work?
Full code:
<script>
    var shared = {
        lock: true,
        connection: '',
        messages: []
    };
    function generateMessage(nick = '', content = '', color = '')
    {
        return {
            time: timeNow(),
            nick: nick,
            content: content,
            color: color
        };
    }
    function timeNow()
    {
        var date = new Date();
        return date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
    }
    export default {
        data() {
            return {
                lock: shared.lock,
                connection: shared.connection,
                login: true,
                message: {
                    time: '',
                    nick: '',
                    content: '',
                    color: 'others'
                },
                messages: shared.messages
            }
        },
        methods:
        {
            submit: function()
            {
                var date = new Date();
                this.message.time = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
                Socket.send(JSON.stringify(this.message));
                this.message.color = '';
                this.messages.push(JSON.parse(JSON.stringify(this.message)));
                this.login = false;
                this.message.color = 'others';
                this.message.content = '';
            }
        }
    }
    var Socket = new WebSocket('ws://localhost:9000/chat');
    Socket.onopen = function(e)
    {
        shared.connection = 'Zostałeś połączony pomyślnie!';
        console.log(shared.connection);
        shared.lock = false;
    };
    Socket.onmessage = function(e)
    {
        shared.messages.push(JSON.parse(e.data));
    };
    Socket.onerror = function(e)
    {
        shared.messages.push(generateMessage('System', 'Wystąpił błąd! Połączenie zostało przerwane.', 'others'));
        shared.lock = true;
    };
    Socket.onclose = function(e)
    {
        shared.messages.push(generateMessage('System', 'Połączenie zostało zamknięte.', 'others'));
        shared.lock = true;
    };
</script>
This question is different because we are facing here VueJS which comes with reactivity and var value can be changed in the future.
Socket.onopen = function(e)
    {
        shared.connection = 'Zostałeś połączony pomyślnie!';
        shared.messages.push(generateMessage('System', 'Wystąpił błąd! Połączenie zostało przerwane.', 'others'));
shared.messages.push(generateMessage('System', 'Wystąpił błąd! Połączenie zostało przerwane.', 'others')); this one works good, messages in Vue updates and I can see it, 
but shared.connection = 'Zostałeś połączony pomyślnie!'; doesn't work, nothing updates in vue... What is going on ? :/
It updates the value, but Vue doesn't get it.
I think it is something with vue..


