Why doesn't the Javascript variable have an object to pass by reference?

I pass an object to a function. I pull out the property of an object, which is a helper object for easy reading. However, this second object does not affect a similar property for the first object. Why is this?

I want processItem.event outside the scope to be updated when the event is saved. Why update processItem, not just a local variable that points to this?

It works:

this.submitForm = function(processItem) {
    var event = processItem.event
    if (event.new) {
        EventDataService.create(event).then(function(response) {
            processItem.event = response.data;
        });
    } else {
        EventDataService.update(event).then(function(response) {
            processitem.event = response.data
        });
    }
};

and it is not

this.submitForm = function(processItem) {
    var event = processItem.event
    if (event.new) {
        EventDataService.create(event).then(function(response) {
            event = response.data;
        });
    } else {
        EventDataService.update(event).then(function(response) {
            event = response.data
        });
    }
};
+4
source share
1 answer
var event = processItem.event

Now eventrefers to the same asprocessItem.event

event = response.data

Now eventrefers to the same as response.data, and no longer refers to the same thing as processItem.event. It is very different from

processItem.event = response.data

processItem.event response.data ( event).

, , JavaScript , .

+7

Source: https://habr.com/ru/post/1614986/


All Articles