gpt4 book ai didi

关于new的javascript内存问题

转载 作者:行者123 更新时间:2023-11-28 11:27:39 25 4
gpt4 key购买 nike

这是我的代码

    function function_to_call(text){
var variable_need = new Array (
0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139,
0x3141, 0x3142, 0x3143, 0x3145, 0x3146, 0x3147,
0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e
);

......

return v;
}
function_to_call(a)
function_to_call(b)
function_to_call(c)

每次调用function_to_call时都会生成variable_need吗?

如果function_to_call频繁调用...

是否需要制作原型(prototype)来防止内存泄漏?

最佳答案

其他人已经回答了您的问题,但您也可以在闭包中保留对该变量的引用,这样它就不会每次都创建,也不是全局变量,但可用于 function_to_call 功能:

var function_to_call = (function() {
var variable_need = [
0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139,
0x3141, 0x3142, 0x3143, 0x3145, 0x3146, 0x3147,
0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e
];
return function (text) {

// variable_need available here

return v;
};
}());

function_to_call(a)
function_to_call(b)
function_to_call(c)

关于关于new的javascript内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6122362/

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