How can I get the memory address of a JavaScript variable?

后端 未结 3 949
南方客
南方客 2020-11-28 09:09

Is it possible to find the memory address of a JavaScript variable? The JavaScript code is part of (embedded into) a normal application where JavaScript is used as a front

相关标签:
3条回答
  • 2020-11-28 09:26

    If it would be possible at all, it would be very dependent on the javascript engine. The more modern javascript engine compile their code using a just in time compiler and messing with their internal variables would be either bad for performance, or bad for stability.

    If the engine allows it, why not make a function call interface to some native code to exchange the variable's values?

    0 讨论(0)
  • 2020-11-28 09:38

    It's more or less impossible - Javascript's evaluation strategy is to always use call by value, but in the case of Objects (including arrays) the value passed is a reference to the Object, which is not copied or cloned. If you reassign the Object itself in the function, the original won't be changed, but if you reassign one of the Object's properties, that will affect the original Object.

    That said, what are you trying to accomplish? If it's just passing complex data between C++ and Javascript, you could use a JSON library to communicate. Send a JSON object to C++ for processing, and get a JSON object to replace the old one.

    0 讨论(0)
  • 2020-11-28 09:51

    I think it's possible, but you'd have to:

    1. download the node.js source code.
    2. add in your function manually (like returning the memory address of a pointer, etc.)
    3. compile it and use it as your node executable.
    0 讨论(0)
提交回复
热议问题