JavaScript variable assignments from tuples

前端 未结 13 1245
[愿得一人]
[愿得一人] 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:25

    As an update to The Minister's answer, you can now do this with es2015:

    function Tuple(...args) {
      args.forEach((val, idx) => 
        Object.defineProperty(this, "item"+idx, { get: () => val })
      )
    }
    
    
    var t = new Tuple("a", 123)
    console.log(t.item0) // "a"
    t.item0 = "b"
    console.log(t.item0) // "a"
    

    https://jsbin.com/fubaluwimo/edit?js,console

    0 讨论(0)
提交回复
热议问题