Chrome控制台中“[Object]”和“[object Object]”之间的区别?

时间:2015-05-04 07:48:54

标签: javascript

我有一些代码如下。

MyRequests.cors_request("POST", 
        APP_CONFIG.APP_URL+"/users/selectAllUsers", 
        null, 
        function ok(users) {  

            $scope.usersNotFiltered = users;

            console.log('users--->', users);
            console.log('$scope.userPerSystem--->', $scope.userPerSystem);

            // delete the items that is already exists in the userPerSystem
            function filterUsers(element, index, array) {

                console.log("$scope.usersNotFiltered :: " + users);
                commonFindAndRemove($scope.usersNotFiltered, 'userId', element.userId);
                console.log('a[' + index + '] = ' + element.userId);
            }

            $scope.userPerSystem.forEach(filterUsers);

            $scope.users = $scope.usersNotFiltered;   
}, 

我运行代码并观察控制台。 chrome控制台中'[Object]'和'[object Object]'有什么区别?

用户---> [对象,对象,对象,对象,对象,对象,对象,对象,对象,对象,对象,对象,对象,对象,对象,对象]

$ scope.userPerSystem ---> [Object] 0:Objectlength:1__proto__:Array [0] redca-ias.concat.js:5258

$ scope.usersNotFiltered :: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[ object object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

1 个答案:

答案 0 :(得分:6)

第一个日志显示结构,即结构的实时表示,第二个日志是结构的字符串表示。

> var arr = [{}, {}, {}];
> arr // simply logs the object as it is
> [Object, Object, Object]
> arr + '' // converts the structure into string
> "[object Object],[object Object],[object Object]"

当您将字符串与对象连接时,您会得到此结果:

"$scope.usersNotFiltered :: " + users

在上面的代码片段中,JavaScript将结构转换为字符串(原始值),对象的字符串表示形式为[object Object]。由于结构是对象的数组,因此每个元素的字符串表示与,连接在一起。您可以通过调用Array.prototype.toString()Array.prototype.join()方法获得类似的结果:

> var arr = [1, 2, 3];
> arr.toString()
> "1,2,3"
> var arrayOfObjects = [{}, {}, {}];
> arrayOfObjects.toString()
> "[object Object],[object Object],[object Object]"