search

Home  >  Q&A  >  body text

javascript - The problem of everything in js being an object

Hello everyone, due to my poor foundation in js, I encountered many basic problems in the project, especially the concept of "everything is an object".
For example:

1,
$scope.dataArr={};
If I want to add attributes to it, $scope.dataArr.currTopic=xxxx is wrong;
However, $scope.dataArr[currTopic] is correct.

2. There is a key named currTopic in
$scope.dataArr, and the initial value is [ ].
I want to add value to the value of currTopic, that is, I want to push elements to [].
Using $scope.dataArr.currTopic.push(xxx) is wrong again.
$scope.dataArr[currTopic].push( xxx ) is correct.

why is that?

为情所困为情所困2712 days ago830

reply all(2)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-16 09:21:22

    The attributes of click access and [] access are the same, and no error will be reported. The test results are as follows:

    reply
    0
  • 欧阳克

    欧阳克2017-06-16 09:21:22

    Please post error message

    var $scope={};
    $scope.dataArr={};
    $scope.dataArr.currTopic="XXXXX";
    $scope.dataArr["currTopic"]="YYYYYY";
    
    $scope.dataArr.currTopic=[];
    $scope.dataArr.currTopic.push("AAAAAAA");
    $scope.dataArr["currTopic"].push("BBBBBB");

    reply
    0
  • Cancelreply