Immutable Hash and Array implementation in JavaScript?

前端 未结 4 944
南方客
南方客 2021-01-05 07:55

Is there simple immutable hash and array implementation in javascript? I don\'t need best speed, a reasonable speed better than a clone would be good.

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 08:35

    The only way to make an object immutable is to hide it inside a function. You can then use the function to return either the default hash or an updated version, but you can't actually store an immutable hash in the global scope.

    function my_hash(delta) {
        var default = {mykey: myvalue};
        if (delta) {
            for (var key, value in delta) {
                if (default.hasOwnProperty(key)) default[key] = value;
            }
        }
        return default;
    }
    

    I don't think this is a good idea though.

提交回复
热议问题