[英]Declaring functions and memory optimization in javascript
I'm declaring a function this way : 我用这种方式声明一个函数:
function clearScrollViewCont(elm,indx,array){
elm.removeAllChildren();
$.scrollView.scrollViewCont.remove(elm);
elm = null;
}
then i call it inside a forEach function : 然后我在forEach函数中调用它:
$.scrollView.scrollViewCont.getchildren().forEach(clearScrollViewCont);
All the previous code is inside another big function so when this big function is removed from the memory all its content will be cleared as well but i don't know what could be the faster way to free memory. 先前的所有代码都在另一个大函数内部,因此当从内存中删除此大函数时,所有内容也会被清除,但我不知道释放内存的更快方法是什么。 I'm used to null every variable which i don't need anymore (even it's written inside a big function) but now, i wonder about clearing memory from declared functions In my previous code, declaring the 'clearScrollViewCont' function that way won't let me to null it later. 我习惯于使不再需要的每个变量都为空(即使它写在一个大函数中),但是现在,我想从声明的函数中清除内存在我之前的代码中,以这种方式声明了“ clearScrollViewCont”函数让我以后再将其取消。 On the other side, if i declare it this way : 另一方面,如果我这样声明:
var clearScrollViewCont = function (elm,indx,array){
elm.removeAllChildren();
$.scrollView.scrollViewCont.remove(elm);
elm = null;
}
i can null it later (simply by nulling the 'clearScrollViewCont' variable) but i don't know if this itself takes more memory space then the first way as now i'm declaring a variable in addition to the function. 我以后可以将其为空(只需通过使'clearScrollViewCont'变量为空)即可,但是我不知道它本身是否会占用更多的存储空间,所以我现在第一种方法是声明函数以外的变量。
What are really pros and cons of each way ? 每种方式的优缺点到底是什么?
In my opinion both ways take up the same amount of space. 我认为两种方式都占用相同的空间。 It shouldn't really matter whether you declare a function like: 是否声明如下函数并不重要:
function clearScrollViewCont(elm,indx,array){}
or 要么
var clearScrollViewCont = function(elm,indx,array){}
In both cases a Function object is created, which is callable by its name: clearScrollViewCont(...);
在这两种情况下,都会创建一个功能对象,该功能对象可以通过其名称调用: clearScrollViewCont(...);
Remember that JavaScript Functions are also objects: ( Functions ) Therefore both ways take up the same amount of space in my opinion. 请记住,JavaScript函数也是对象:( 函数 )因此,我认为两种方法都占用相同的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.