JavaScript不可变的数据结构:seamless-immutable

jopen 9年前

JavaScript的不可变数据结构,向后兼容正常的JS Arrays 和 Objects。

Use them in for loops, pass them to functions expecting vanilla JavaScript data structures, etc.

var array = Immutable(["totally", "immutable", {hammer: "Can’t Touch This"}]);    array[1] = "I'm going to mutate you!"  array[1] // "immutable"    array[2].hammer = "hm, surely I can mutate this nested object..."  array[2].hammer // "Can’t Touch This"    for (var index in array) { console.log(array[index]); }  // "totally"  // "immutable"  // { hammer: 'Can’t Touch This' }    JSON.stringify(array) // '["totally","immutable",{"hammer":"Can’t Touch This"}]'

This level of backwards compatibility requires ECMAScript 5 features like Object.defineProperty and Object.freeze to exist and work correctly, which limits the browsers that can use this library to the ones shown in the test results below. (tl;dr IE9+)

JavaScript不可变的数据结构:seamless-immutable


 

项目主页:http://www.open-open.com/lib/view/home/1415696467805