gpt4 book ai didi

javascript - 创建新数组和清除长度之间的区别

转载 作者:搜寻专家 更新时间:2023-11-01 05:29:28 25 4
gpt4 key购买 nike

<分区>

如果我必须删除数组中的所有项目,使用new Arraylength = 0 有什么区别?在这个例子中,显然结果看起来是一样的。

function MyCtrl($scope) { 

$scope.arrayData = ["1", "2", "3"];

$scope.newArray1 = function(){
$scope.arrayData = new Array();
}

$scope.newArray2 = function(){
$scope.arrayData = [];
}

$scope.lengthZero = function(){
$scope.arrayData.length = 0;
}

$scope.populate = function(){
$scope.arrayData.push("1", "2", "3");
}
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app ng-controller="MyCtrl">
Data:
</h2><br>
<ul>
<li ng-repeat="data in arrayData">{{data}}</li>
</ul>
<button ng-click="newArray1()">New Array - 1st method</button>
<button ng-click="newArray2()">New Array - 2nd method</button>
<button ng-click="lengthZero()">Length = 0</button>

<button ng-click="populate()">Populate</button>
</div>

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com