News for hackers. Stuff that matters.
Re:KindScript – A kinder JavaScript experience by amelius on Sunday March 20, 2016 @10:49AM (#11323119)
I want a Javascript that does this right:

    var m = {};
    m["__alpha__"] = 10;
    m["__bravo__"] = 20;
    m["__proto__"] = 30;
    for(var k in m) console.log(k);
Result (in current JS):

    __alpha__
    __bravo__
EDIT: To be clear, I want a Javascript that is consistent. Imho, the fact that __proto__ is a property is a hack. Also, I suspect that a lot of software written in Javascript is susceptible to injection bugs/attacks by using the string "__proto__" as input in various places.
Re:KindScript – A kinder JavaScript experience by dchest on Sunday March 20, 2016 @12:10PM (#11323414)
What you want is to use is a map, not a JavaScript object. Here you go:

    const m = new Map();
    m.set("__alpha__", 10);
    m.set("__bravo__", 20);
    m.set("__proto__", 30);
    for (let k of m.keys()) {
       console.log(k);
    }
Re:KindScript – A kinder JavaScript experience by ankjevel on Sunday March 20, 2016 @11:10AM (#11323211)
Why would you like to add anything to the __proto__ property? One way would be to write, like this:

  {
    const m = {}

    m.__alpha__ = 10
    m.__bravo__ = 20
    m.__defineGetter__('__proto__', () => 30)
    
    Reflect.ownKeys(m).forEach(key => console.log(key))
  }
EDIT: I can't format ...
Re:KindScript – A kinder JavaScript experience by amelius on Sunday March 20, 2016 @11:22AM (#11323255)
To make my point clear, here is a question. What is wrong with the following function that counts the number of unique strings in a list of strings?

    function count_unique_strings(l)
    {
        var m = {};

        l.forEach(function(s)
        {
            m[s] = true;
        });

        var count = 0;
        for(var k in m) ++count;

        return count;
    }
To see why this function is incorrect, run it with the following input:

    console.log(count_unique_strings(["alpha", "bravo", "bravo", "charlie"]));
    console.log(count_unique_strings(["alpha", "bravo", "bravo", "__proto__"]));
Re:KindScript – A kinder JavaScript experience by dchest on Sunday March 20, 2016 @12:12PM (#11323425)
What is wrong with the following function

The implementation is wrong: you're using Object when you want to use Map. Object is not suitable for this, and seems like you know it, and it's in language specs.

Re:KindScript – A kinder JavaScript experience by amelius on Sunday March 20, 2016 @12:36PM (#11323529)
Yes, I stated beforehand that the implementation is wrong. My point is that it is not obvious from the implementation (and certainly not for beginners).
Re:KindScript – A kinder JavaScript experience by pluma on Sunday March 20, 2016 @12:19PM (#11323457)
You're thinking of `Map` not `Object`.

Objects can be used as cheap maps but they're more than that. That's why ES2015 brought us Map and Set.

Re:KindScript – A kinder JavaScript experience by joshuacc on Sunday March 20, 2016 @11:42AM (#11323321)
var m = Object.create(null);

Problem solved.

Re:KindScript – A kinder JavaScript experience by chowells on Sunday March 20, 2016 @11:49AM (#11323345)
That solution doesn't cast javascript in a positive light.

Why should you need to know that much about the guts of the language just to create an associative array that works properly?

Re:KindScript – A kinder JavaScript experience by joshuacc on Sunday March 20, 2016 @12:11PM (#11323421)
JavaScript objects aren't associative arrays. They inherit property values from their prototype. Not just __proto__, but hasOwnProperty, etc. If you want to abuse objects in this way, it makes sense that you need a special approach to opt out of inheritance.

A better approach is to just use Map.

var m = new Map();

Re:KindScript – A kinder JavaScript experience by amelius on Sunday March 20, 2016 @01:04PM (#11323632)
It would have been so much better if inheritance was opt-in rather than opt-out.
Re:KindScript – A kinder JavaScript experience by ricardobeat on Sunday March 20, 2016 @06:52PM (#11325093)
Both Map and Symbol already solve this in ES6.