JavaScript variable assignments from tuples

前端 未结 13 1244
[愿得一人]
[愿得一人] 2020-12-04 18:26

In other languages like Python 2 and Python 3, you can define and assign values to a tuple variable, and retrieve their values like this:

tuple = (\"Bob\", 2         


        
相关标签:
13条回答
  • 2020-12-04 19:07

    Here is a simple Javascript Tuple implementation:

    var Tuple = (function () {
       function Tuple(Item1, Item2) {
          var item1 = Item1;
          var item2 = Item2;
          Object.defineProperty(this, "Item1", {
              get: function() { return item1  }
          });
          Object.defineProperty(this, "Item2", {
              get: function() { return item2  }
          });
       }
       return Tuple;
    })();
    
    var tuple = new Tuple("Bob", 25); // Instantiation of a new Tuple
    var name = tuple.Item1; // Assignment. name will be "Bob"
    tuple.Item1 = "Kirk"; // Will not set it. It's immutable.
    

    This is a 2-tuple, however, you could modify my example to support 3,4,5,6 etc. tuples.

    0 讨论(0)
  • 2020-12-04 19:10

    A frozen array behaves identically to a python tuple:

    const tuple = Object.freeze(["Bob", 24]);
    let [name, age]; = tuple
    console.debug(name); // "Bob"
    console.debug(age); // 24
    

    Be fancy and define a class

    class Tuple extends Array { 
      constructor(...items) { 
        super(...items); 
        Object.freeze(this);
      } 
    }
    
    let tuple = new Tuple("Jim", 35);
    let [name, age] = tuple;
    console.debug(name); // Jim
    console.debug(age); // 35
    tuple = ["Bob", 24]; // no effect 
    console.debug(name); // Jim
    console.debug(age); // 25
    

    Works today in all the latest browsers.

    0 讨论(0)
  • 2020-12-04 19:14

    You have to do it the ugly way. If you really want something like this, you can check out CoffeeScript, which has that and a whole lot of other features that make it look more like python (sorry for making it sound like an advertisement, but I really like it.)

    0 讨论(0)
  • 2020-12-04 19:15

    You can have a tuple type in Javascript as well. Just define it with higher order functions (the academic term is Church encoding):

    const Tuple = (...args) => {
      const Tuple = f => f(...args);
      return Object.freeze(Object.assign(Tuple, args));
    };
    
    const get1 = tx => tx((x, y) => x);
    
    const get2 = tx => tx((x, y) => y);
    
    const bimap = f => g => tx => tx((x, y) => Tuple(f(x), g(y)));
    
    const toArray = tx => tx((...args) => args);
    
    // aux functions
    
    const inc = x => x + 1;
    const toUpperCase = x => x.toUpperCase();
    
    // mock data
    
    const pair = Tuple(1, "a");
    
    // application
    
    console.assert(get1(pair) === 1);
    console.assert(get2(pair) === "a");
    
    const {0:x, 1:y} = pair;
    console.log(x, y); // 1 a
    
    console.log(toArray(bimap(inc) (toUpperCase) (pair))); // [2, "A"]
    
    const map = new Map([Tuple(1, "a"), Tuple(2, "b")]);
    console.log(map.get(1), map.get(2)); // a b

    Please note that Tuple isn't used as a normal constructor. The solution doesn't rely on the prototype system at all, but solely on higher order functions.

    What are the advantages of tuples over Arrays used like tuples? Church encoded tuples are immutable by design and thus prevent side effects caused by mutations. This helps to build more robust applications. Additionally, it is easier to reason about code that distinguishes between Arrays as a collection type (e.g. [a]) and tuples as related data of various types (e.g. (a, b)).

    0 讨论(0)
  • 2020-12-04 19:16

    Unfortunately you can't use that tuple assignment syntax in (ECMA|Java)Script.

    EDIT: Someone linked to Mozilla/JS 1.7 - this wouldn't work cross-browser but if that is not required then there's your answer.

    0 讨论(0)
  • 2020-12-04 19:20

    Tuples aren't supported in JavaScript

    If you're looking for an immutable list, Object.freeze() can be used to make an array immutable.

    The Object.freeze() method freezes an object: that is, prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed. In essence the object is made effectively immutable. The method returns the object being frozen.

    Source: Mozilla Developer Network - Object.freeze()

    Assign an array as usual but lock it using 'Object.freeze()

    > tuple = Object.freeze(['Bob', 24]);
    [ 'Bob', 24 ]
    

    Use the values as you would a regular array (python multi-assignment is not supported)

    > name = tuple[0]
    'Bob'
    > age = tuple[1]
    24
    

    Attempt to assign a new value

    > tuple[0] = 'Steve'
    'Steve'
    

    But the value is not changed

    > console.log(tuple)
    [ 'Bob', 24 ]
    
    0 讨论(0)
提交回复
热议问题