5 ms·
Javascript Pointers
- jerf 15y agoThis is either bad satire, or a bad idea.
- technomancy 15y agoAre we ruling out good satire? Because, I mean... wow.
- jerf 15y agoI've done my time helping people out on various fora, mostly comp.lang.python, and seen this sort of thing in reality. If it's satire, the tipoff is too small. I've seen "stupider" (or "beginninger" or some other term, if it makes you fell better, I haven't got a good one).
- adrusi 15y agonot satire, but not a bad idea either, I wrote this as a proof of concept, not as something intended to be used.
- paulbaumgart 15y agoPsh, Cappuccino had this over a year ago: http://news.ycombinator.com/item?id=1233681 http://news.ycombinator.com/item?id=1233681
- deleted 15y ago[deleted]
- adrusi 15y agoyes, that's where I got the idea from, I just wanted to make the usage a bit nicer
- deweller 15y agoNext up: malloc Ok - now I'm convinced it is a joke. Dry humor, indeed.
- city41 15y agoI don't think it's a joke. We don't have much context to go on. I get the feeling the author was just exploring/experimenting. Implementing "pointers" and some sort of malloc in javascript probably has very little practical application, but the author may learn a bit more about how memory works. Maybe that is his real goal.
- adrusi 15y agoyes, that was a joke :)
- gildas 15y agoThere are other solutions: http://brandon.pulpexplosion.com/index.php/2010/05/javascript-pointers/ http://brandon.pulpexplosion.com/index.php/2010/05/javascrip...
- boucher 15y agoIt's not exactly fair to call either technique pointers, but the original could be useful for more things than just this (though this is probably the intended use case). The linked article, however, makes no sense. The author does not seem to understand how JavaScript works. Arrays are not copied when passed to another function.
- anc2020 15y agoThis article is incorrect, arrays are passed by reference since they are objects. I've added a comment on the blog.
- KirinDave 15y agoWhat problem does this solve? What problem do either of these ideas solve? Is the question, "What is the best way to troll programmers on the internet?"
- gildas 15y agoI wish it was the case...
- flitzwald 15y agoThe only problem I see with this approach is the Javascript-interpreter seeing through your pointer system and holding a reference to the object pointed to - which defeats the purpose of creating a possibility to segfault...
- Andi 15y agoThere are real references in JavaScript - when you pass objects. This case is completely ignored. I want to get the same pointer when it's the same object, even if the properties have changed. This is stubborn. I have solved this problem in a library - and you need to add the id to the object itself. Anyway, explicit pointers are pretty "pointless" in JavaScript and should only used for recursion detection.
- anc2020 15y agoSometimes you really do just want to pass a value by reference. It turns out that most of the time you want to do this the variable already is an object or array already, nevertheless there are cases where this would be desirable, so it's not a bad idea when used carefully. Having said that, malloc is a bad idea and completely unnecessary, and a nicer way to do the functionality might be to do: function pmk(val) { return { v: val }; } function pget(p) { return p.v; } function pset(p, val) { return p.v = val; } Doesn't need free'ing, at the minimal expense of creating one object per "pointer". Neither of the solutions give you an "address of" type behaviour though :(
- kungfooguru 15y agoIt has to be a joke. Its just a closure.
- KirinDave 15y agoWhat this article taught me is that some javascript programmers do not understand pointers. Considering how hard we have fought to liberate programmers from pointers, I am having mixed feelings about this lesson. One the one hand, what a paradise we live in that this person can imagine these sterile, useless contrivances are pointers. On the other hand, experience bij.
- sktrdie 15y agoThere's nothing wrong with this. It's a high level implementation of a memory system. The memory locations are represented as a JSON object: { 1: "some data", 2: "foo" ... } It's not really needed in JavaScript but I remember implementing something similar in C for school.
- M1573RMU74710N 15y agoHey, sorry to be pedantic but this is one of my pet peeves. "JSON" is a way to serialize data to in a string. It's based on Javascript's object literal syntax. JSON has absolutely nothing to do with this article. What you mean is the memory locations are stored in an Object (or if you want to be specific about the language "a Javascript Object) If you want to refer to Javascript's handy way of creating objects like so: var x = {}; That is an "object literal" (which also has little to do with the article) I'd also note that there is something wrong with this as they could get the same behavior (only faster, smaller, and cross-browser) by just using an Array.
- anc2020 15y agoI appreciate your enthusiasm for correctness, but if I'm reading correctly, the essence of sktrdie's comment was trying to counter the group think that the idea of the article is in some way "bad", "satirical", or "wrong". It may be that I only ever merit to use this pattern once every 10,000 lines, but that in no way makes the idea of calling with reference over primitive values incorrect. Readers should understand the difference between rare and never.
- M1573RMU74710N 15y ago> he essence of sktrdie's comment Sure, however the first part of my comment is not intended to address the essence of their comment, so it has no bearing. What they said is incorrect, and in fact a common mistake and I wanted to correct it. However, I'd also note that what sktrdie is describing is an Array (in Javascript an Array is just an object with numbers for property names and some helper methods). As far as I can see there is absolutely zero benefit (and many drawbacks) in using the technique outline in the OP article as opposed to just using an Array. You could get the same result by simply using an Array directly...or if you want to get fancy, add a few simple helper methods. In this way (and perhaps others) the article is "bad" and "wrong".
- M1573RMU74710N 15y agoThe "free" function as written won't work. When using Object.defineProperty if "configurable" is not set, the default is false (at least in Firefox, I don't recall what the spec says but I think it's just toBoolean whatever calling [[getownproperty]] "configurable" on the description object is, which would default to false), meaning the property cannot be deleted (or redefined). (I'd also note not all browsers support defineProperty) I'd also note to readers that the "delete" operator just removes a property name from an objects own property list, and has little to do with garbage collection directly. In this case I think the goal is to get rid of the closure created which handles the property, which I think would work (if the previous error is fixed), but I'm not familiar enough with JS engine internals to know. I would probably just redefine the property to be "null". Once the property is null'd out, there's probably not much benefit to also removing the property from the property list. With it null'd out as opposed to deleted you'd also not have to worry about an Object up the prototype chain punching through, and might be desirable in other ways. Also note that this will not necessarily free up whatever was stored in the "pointer". I don't think that was the author's intention, but I just wanted to make that clear to readers. If you did: var foo = {}; foo = $(foo); bar = $[foo]; $.free(foo); The object created on the first line would not be garbage collected after free is called because bar now contains a reference to it. If your goal was to clear up whatever was stored in "val" as well as the closure itself, you'd have to first set the property to null (to take care of "val"), and then redefine the property to null (to take care of the closure). Of course, the biggest problem here is that this is pointless. They are storing the value in a closure, but since they just expose the closure with get and set methods, they could just skip it all and us an Array.
- adrusi 15y agoYes I suppose `configurable` should be enabled. The reason I used delete instead of setting to null is that null is considered just as important a value as any other and if you had an outrageous number of nulls, it could consume a noticeable amount of memory. `free` only releases the data stored in the global pointer object, not the original variable itself, that's not possible. I don't know how many time's I've had to say this, but this was never intended to actually be used, just as a proof of concept.
- adrusi 15y agoI wrote this post, and am alarmed by the publicity it's getting and the controversy it's causing. I NEVER meant this to be used to solve any problem, nor to be used in a real life situation at all. I didn't mean this as a joke or as satire. I wrote the as a PROOF OF CONCEPT, just to show some capabilities of javascript.