2015-05-04 133 views
6

我有一些像下面的代碼。Chrome控制檯中'[Object]'和'[object Object]'的區別?

MyRequests.cors_request("POST", 
     APP_CONFIG.APP_URL+"https://stackoverflow.com/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; 
}, 

我運行tihs代碼並觀看控制檯。在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]

+0

是什麼使它有所不同? $ scope.usersNotFiltered和用戶是同一類型的值,我從$ http回調返回值。 – verystrongjoe

回答

4

第一個日誌顯示了它的結構,即結構的實時表示,第二個日誌顯示結構的字符串表示。

"$scope.usersNotFiltered :: " + users 

在上面的代碼中的JavaScript結構轉換成字符串(原始值)和字符串表示的:

> 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]" 

你,你正在連接具有一個對象的字符串得到這樣的結果一個對象是[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]" 
相關問題