简体   繁体   English

如果我从不调用回调,JavaScript中会发生什么?

[英]What happens in JavaScript if I never call a callback?

Suppose I have the following code 假设我有以下代码

function myFunction(param, callback) {
    ...
    if (err) {
        console.log("error");
        console.log(err);
    }
    else {
        callback(data);
    }
}

In the case of no error, the callback is called. 在没有错误的情况下,调用回调。 In the case of an error, it is not. 如果出现错误,则不是。 Suppose the calling function looks something like the following 假设调用函数类似于以下内容

myFunction(param, function(data) {
...
});

Are there memory leak issues or similar? 是否有内存泄漏问题或类似问题? Is there a better way to handle this scenario? 有没有更好的方法来处理这种情况?

A JavaScript object will not be eligible for reclamation as long as it is strongly-reachable: that is, if it can be reached by traversing the object graph from a root object (which basically amounts to a global property or, possibly closed-over, variable). JavaScript对象将不符合用于回收,只要它是强可达:即,如果它可以通过从一个根对象遍历对象图来达到(其基本上相当于一个全局属性,或者可能封闭结束后,变量)。 Any object that is no longer strongly reachable is no longer accessible via JavaScript and will be reclaimed by the GC (when the GC feels like it). 任何不再强烈可访问的对象都不再可以通过JavaScript访问,并且将由GC回收(当GC感觉像这样)。

In this case the function-object (callback) passed to myFunction is only strongly-reachable for the duration of the function call when it is accessible via the callback parameter*. 在这种情况下,传递给函数的对象(回调) myFunction 只有强可达的函数调用的持续时间时,它是通过访问callback参数*。 Because the function-object is not strongly-reachable after the function (eg it was not saved to a global property) then the function-object is eligible for reclamation - along with any function scopes that it referenced should they no longer be strongly-reachable - as soon as the function terminates. 因为在函数之后函数对象不是强可达的(例如,它没有保存到全局属性),所以函数对象有资格进行回收 - 以及它引用的任何函数作用域,如果它们不再是强可达的 - 一旦功能终止。

Thus in this case , there is no "memory leak". 因此, 在这种情况下 ,没有“内存泄漏”。 However, imagine this case: 但是,想象一下这种情况:

window.myCallbacks = []
function myFunction(param, callback) {
    ...
    window.myCallbacks.push(callback)  // hmm, maybe always strongly-reachable?
}

Happy coding. 快乐的编码。


Technically, a real smart JavaScript engine could determine that the object named by callback was no longer strongly-reachable (via callback ) in the "if" branch. 从技术上讲,一个真正的智能JavaScript引擎可以确定callback命名的对象在“if”分支中不再是强可达的(通过callback )。 I am not sure if any of the JS engines actually go this far, but the question becomes more interesting when talking about function scopes bound in closures (and if this keeps all the objects named by all the variables, even those not accessed later, strongly-reachable). 我不确定是否有任何JS引擎实际上走得这么远,但是在讨论闭包中绑定的函数作用域时问题会变得更有趣(如果这样可以保留所有变量命名的所有对象,即使是后来未被访问的那些变量,也是如此-reachable)。

Adding to the above answer see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management . 添加上述答案请参阅: https//developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management Where it says: 在哪里说:

This algorithm reduces the definition of "an object is not needed anymore" to "an object is unreachable". 该算法将“不再需要对象”的定义减少为“对象不可达”。

This algorithm assumes the knowledge of a set of objects called roots (In JavaScript, the root is the global object). 该算法假定知道一组称为根的对象(在JavaScript中,根是全局对象)。 Periodically, the garbage-collector will start from these roots, find all objects that are referenced from these roots, then all objects referenced from these, etc. Starting from the roots, the garbage collector will thus find all reachable objects and collect all non-reachable objects. 垃圾收集器会定期从这些根开始,找到从这些根引用的所有对象,然后是从这些根引用的所有对象,等等。从根开始,垃圾收集器将找到所有可到达的对象并收集所有非对象可达物体。

No need to worry about leaking memory with out calling a callback. 无需担心在调用回调时泄漏内存。 You may want to have an error callback. 您可能希望有一个错误回调。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我在 javascript 中的单个脚本标记内调用 2 个函数会发生什么? - what happens if I call 2 functions inside a single script tag in javascript? 调用服务器后javascript变量会如何处理? - what happens to javascript variable after a call to the server? 在这种特殊情况下(通过共享调用),Javascript 会发生什么? - What happens in Javascript in this particular case (Call by sharing)? 当我调用带有参数的Javascript函数而不提供这些参数时会发生什么? - What happens when I call a Javascript function which takes parameters, without supplying those parameters? 如果我不 removeEventListener (JavaScript) 会发生什么? - What happens if I don't removeEventListener (JavaScript)? 当脚本执行时AJAX调用返回时,JavaScript会发生什么? - What happens in JavaScript when an AJAX call returns while the script is executing? 如果在JavaScript中单击按钮事件时调用两个函数会发生什么 - What happens if call two functions when button click event in javaScript 用于Javascript的Facebook SDK,FB.login()回调未触发(异步调用永远不会得到响应?) - Facebook SDK for Javascript, FB.login() callback not firing (async call never gets a response?) 我可以决定调用不存在的对象方法时会发生什么吗? - Can I decide what happens on a call to a nonexistent object method? 如果我在页面中合并多个Javascript库,会发生什么情况? - What happens If I combine multiple Javascript Libraries in a page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM