[英]how to clear previous data from variable if i don't know the type of variable(may be array,object or primitive)
Hi i am new in angular js or javascript.i don 't know how to clear previous data of variable,if i dont know type of variable ? 嗨,我是angular js或javascript的新手。如果我不知道变量的类型,我不知道如何清除变量的先前数据?
Comment.get()
.success(function(data) {
//here i have to clear previous data of $scope.data, but i dont know type of $scope.data
$scope.data = data;
$scope.loading = false;
});
could you please suggest me how to clear previous data from variable $scope.data ? 您能否建议我如何从变量$ scope.data中清除以前的数据?
In JavaScript there are no declaration of types of variable. 在JavaScript中,没有变量类型的声明。 So whatever the type of data that a variable contains it can be overwritten by any other types of data. 因此,变量包含的任何数据类型都可以被任何其他类型的数据覆盖。
And by your function, its good as it is. 根据您的功能,它确实是好的。
Depends on what you mean by "clear", but the typical way to clear a variable is like this: 取决于“清除”的含义,但是清除变量的典型方法如下:
delete $scope.data; 删除$ scope.data;
Then if you need to check that the variable is empty/exists you can do: 然后,如果您需要检查变量是否为空/存在,则可以执行以下操作:
if ($scope.data !== undefined) 如果($ scope.data!==未定义)
...or whatever. ...管他呢。 You don't need to assign the variable as an array or string when "clearing" it. “清除”变量时,无需将变量分配为数组或字符串。 JavaScript is not strongly typed. JavaScript不是强类型的。
However, as someone already pointed out in the comments you probably don't need to clear the variable at all because it is overwritten every time. 但是,正如有人已经在注释中指出的那样,您可能根本不需要清除该变量,因为它每次都会被覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.