简体   繁体   English

JavaScript中的对象值和对象数组

[英]Objects value and object arrays in JavaScript

I am developing a web application in AngularJS. 我正在AngularJS中开发Web应用程序。 I am confused about references in JavaScript. 我对JavaScript中的引用感到困惑。 When do changes on an object affect its references? 对象上的更改何时会影响其引用?

For example, I have a controller in my app. 例如,我的应用程序中有一个控制器。 and I use that controller to upload files and keep file information in a factory. 我使用该控制器上传文件并将文件信息保存在工厂中。

   $scope.fileArray = [];
    ...
    ...
    uploadFiles(file){
         $scope.fileArray.push(file);
         ..
         ..
         //on success
         file.params = data["parameters"]; //an array of strings about file path and name
         ...
         ...
    }

In uploadFiles function, I push file object into fileArray. 在uploadFiles函数中,我将文件对象推入fileArray中。 Then when I change or add new attributes to file object, I can see those changes in fileArray. 然后,当我更改或向文件对象添加新属性时,可以在fileArray中看到这些更改。

var fileInfo = {
    infoArray : $scope.fileArray
}

fileFactory.keepFileInfo(fileInfo);

then I keep that array in factory. 然后我将该阵列保留在工厂中。 But this time change on $scope.fileArray is not reflected in factory. 但这一次$ scope.fileArray的更改未反映在工厂中。 What is the logic here? 这里的逻辑是什么?

EDIT 编辑

I was wrong. 我错了。 Change in controller object effects factory, too. 控制器对象的效果工厂的变更也。 I wrote this minimal application to see it clearly and in my application I must be making error some where else. 我写了这个最小的应用程序以清楚地看到它,并且在我的应用程序中我必须在其他地方犯错误。

var testApp = angular.module('testApp', []);

testApp.controller('TestController', function($scope, TestFactory) {
    $scope.fileArray = [];

    var files = [];
    files[0] = {
        parameters : {
            name : 'file1.txt',
            path : "user/files"
        }
    }

    $scope.fileArray.push(files[0]);

    files[1] = {
        parameters : {
            name : 'file2.txt',
            path : "user/files"
        }
    }

    $scope.fileArray.push(files[1]);

    files[2] = {
        parameters : {
            name : 'file3.txt',
            path : "user/files"
        }
    }

    $scope.fileArray.push(files[2]);

    TestFactory.setFileArray($scope.fileArray);

    files[0].parameters["name"] = "changed file name";

    console.log($scope.fileArray); //here writes "changed file name"    
    console.log(TestFactory.getFileArray()); //here writes "changed file name", too
});


testApp.factory('TestFactory', function() {
    var factory = {};

    var fileArray = [];

    factory.setFileArray = function(files) {
        fileArray = files;
    }

    factory.getFileArray = function() {
        return fileArray;
    }

    return factory;
});

Let's consider the following example: 让我们考虑以下示例:

var a = {};
var b = {c: a};
console.log(a === b.c);
a.d = {};
console.log(b.c);

If you have an object variable and you assign the member of another object to it, then they have the very same reference, so if you change either one, the other one will be changed as well. 如果您有一个对象变量,并且将另一个对象的成员分配给它,则它们具有完全相同的引用,因此,如果您更改其中一个,则另一个也将更改。 The same is the case if you do this with arrays: 如果对数组执行此操作,则情况相同:

var a = {};
var b = [a];
console.log(a === b[0]);
a.d = {};
console.log(b[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM