简体   繁体   English

如果我不知道变量的类型,如何从变量中清除以前的数据(可能是数组,对象或基元)

[英]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.

相关问题 如果我不知道 javascript 中的完整变量名,如何找到变量/数组 - How find variable/array if I don't know full variable name in javascript 我想在 postgress 中插入一个数组类型的数组,但我不知道如何从节点执行它 - I want to insert an array of type array into postgress but I don't know how to do it from node 如何在不知道深度级别的情况下将数据作为对象插入嵌套对象数组中 - How can I insert the data as object in array of nested object while I don't know it's depth level 我不知道如何正确地从 fetch 中获取数据 - I don't know how to get data from fetch correctly 如果我不知道要返回什么,如何整齐地输出json对象中的所有数据? - How do I neatly output all the data from a json object if I don't know what I'm going to get back? 如何获取我不知道其名称的变量的内容 - How can I get the content of a variable that I don't know its name 如何从将来的函数调用中清除先前的变量 - how to clear previous variable from future function calls 可变大小的JavaScript字符串如何是原始类型? - How is the variable sized JavaScript string a primitive type? 我不知道如何将变量中的每个数据值赋予另一个变量中的值 - I dont know how to give each value of data from a variable to a value from another variable 如何在将变量发送到PHP的同时在Ajax调用期间清除缓存中的先前变量 - How do I clear previous variable in cache during the Ajax call while sending variable to PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM