Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7852597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:27:46+00:00 2026-06-02T19:27:46+00:00

I’m using dynamic scoping to simulate pointers in JavaScript as follows : var ptr

  • 0

I’m using dynamic scoping to simulate pointers in JavaScript as follows:

var ptr = (function () {
    var ptr = "(" + String(function (value) {
    if (value === void 0) return upvalue;
    else upvalue = value;
}) + ")";

    return function (upvalue) {
        return ptr.replace(/upvalue/g, upvalue);
    };
}());

function swap(xptr, yptr) {
    var t = xptr();
    xptr(yptr());
    yptr(t);
}

var x = 2;
var y = 3;

alert([x, y]);
swap(eval(ptr("x")), eval(ptr("y")));
alert([x, y]);

Is there any other way to achieve the same results (i.e. without resorting to eval)? It just seems like too much boilerplate.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:27:47+00:00Added an answer on June 2, 2026 at 7:27 pm

    Since the only thing you’re using the pointer for is to dereference it to access another variable, you can just encapsulate it in a property.

    function createPointer(read, write) {
      return { get value() { return read(); }, set value(v) { return write(v); } };
    }
    

    To create a pointer, pass the accessor methods which read and write the variable being pointed to.

    var i;
    var p = createPointer(function() { return i; }, function(v) { i = v; });
    // p is now a "pointer" to i
    

    To dereference a pointer, access its value. In other words, where in C you would write *p here you write p.value.

    i = "initial";
    alert(p.value); // alerts "initial"
    p.value = "update";
    alert(i); // alerts "update"
    p.value += "2";
    alert(i); // alerts "update2"
    

    You can create multiple pointers to the same variable.

    var q = createPointer(function() { return i; }, function(v) { i = v; });
    // q is also a "pointer" to i
    alert(q.value); // alerts "update2"
    q.value = "written from q";
    alert(p.value); // alerts "written from q"
    

    You can change what a pointer points to by simply overwriting the pointer variable with another pointer.

    var j = "other";
    q = createPointer(function() { return j; }, function(v) { j = v; });
    // q is now a "pointer" to j
    

    You can swap two variables through pointers.

    function swap(x, y) {
        var t = x.value;
        x.value = y.value;
        y.value = t;
    }
    

    Let’s swap the values of i and j by using their pointers.

    swap(p, q);
    alert(i); // alerts "other"
    alert(j); // alerts "written from q"
    

    You can create pointers to local variables.

    function example() {
        var myVar = "myVar as local variable from example";
        var r = createPointer(function() { return myVar; }, function(v) { myVar = v; });
        swap(p,r);
        alert(i); // alerts "myVar as local variable from example"
        alert(myVar); // alerts "other"
    }
    example();
    

    Through the magic of closures, this gives you a way to simulate malloc.

    function malloc() {
        var i;
        return createPointer(function() { return i; }, function(v) { i = v; });
    }
    var p = malloc(); // p points to a variable we just allocated from the heap
    p.value = 2; // write a 2 into it
    

    Your magic trick works too:

    var flowers = new Misdirection(
           createPointer(function() { return flowers; }, function(v) { flowers = v; }));
    flowers.abracadabra();
    alert(flowers);
    
    function Misdirection(flowers) {
        this.abracadabra = function() {
            flowers.value = new Rabbit;
        };
    }
    
    function Rabbit() {
        this.toString = function() { return "Eh... what's up doc?" };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.