标签: destructuring

为什么这是有效的语法?

在重构一些代码时,我偶然发现这是有效的语法(或者至少不会导致 Firefox 中的解析器错误):

const {} = somefunc();

somefunc返回 anobject并且 curl 应该包含用于解构的变量名称,当时我还没有决定这些名称是什么,所以我暂时将它们留空。

编辑器没有突出显示语法错误,所以出于好奇我对其进行了测试,并且很惊讶 Firefox 实际上对这种语法没有问题。

为什么这种语法有效?它真的做了一些奇怪的事情吗?

javascript destructuring

29
推荐指数
2
解决办法
730
查看次数

使用扩展和解构运算符修改不可变对象的最短方法是什么?

我正在寻找一个纯函数,来修改我的不可变状态对象.作为参数给出的原始状态必须保持不变.这在使用像Redux这样的框架时非常有用,并且使得在javascript中使用不可变对象变得更加容易.特别是因为使用Babel处理对象传播操作符已经成为可能.

我没有找到比首先复制对象更好的东西,而是分配/删除我想要的属性:

function updateState(state, item) {
  newState = {...state};
  newState[item.id] = item;
  return newState;
}

function deleteProperty(state, id) {
    var newState = {...state};
    delete newState[id];
    return newState;
}
Run Code Online (Sandbox Code Playgroud)

我觉得它可能会更短

javascript destructuring ecmascript-next

26
推荐指数
2
解决办法
1万
查看次数

如果它们位于单独的命名空间中,如何将我的规范用于其预期目的?

clojure.spec指南中的一个示例是一个简单的选项解析规范:

(require '[clojure.spec :as s])

(s/def ::config
  (s/* (s/cat :prop string?
              :val (s/alt :s string? :b boolean?))))

(s/conform ::config ["-server" "foo" "-verbose" true "-user" "joe"])
;;=> [{:prop "-server", :val [:s "foo"]}
;;    {:prop "-verbose", :val [:b true]}
;;    {:prop "-user", :val [:s "joe"]}]
Run Code Online (Sandbox Code Playgroud)

稍后,在验证部分中,定义了一个函数,该函数conform使用此规范在内部输入:

(defn- set-config [prop val]
  (println "set" prop val))

(defn configure [input]
  (let [parsed (s/conform ::config input)]
    (if (= parsed ::s/invalid)
      (throw (ex-info "Invalid input" (s/explain-data ::config input)))
      (doseq [{prop …
Run Code Online (Sandbox Code Playgroud)

namespaces clojure destructuring cyclic-dependency clojure.spec

26
推荐指数
1
解决办法
1887
查看次数

Destructure parameter of a Clojure function while keeping the original value.

Can you destructure a function parameter but still have the original available for use? The way I'm doing it now is just using a let form inside the function body, but I wondering if there was a terser way of doing it.

clojure destructuring

24
推荐指数
1
解决办法
8141
查看次数

我可以使用与immutable.js的解构赋值吗?

使用标准JS对象,可以使用解构赋值,例如:

let obj = {name: 'james', code: '007'}
let {name, code} = obj // creates new variables 'name' and 'code' (with the proper values)
Run Code Online (Sandbox Code Playgroud)

正如一些Flux/Redux布道者的建议,我使用immutable.js作为我的应用程序; 我可以在不可变的List/Map上使用解构吗?当然,人们可以这样做:

let obj = immutable.fromJS({name: 'james', code: '007'})
let {name, code} = obj.toJS()
Run Code Online (Sandbox Code Playgroud)

但随着对象变大,这似乎效率很低(因为对象需要先被深深地理解).

destructuring immutable.js

24
推荐指数
1
解决办法
6401
查看次数

在php中为对象/关联数组进行解构赋值

在CoffeeScript,Clojure,ES6和许多其他语言中,我们对对象/贴图/等进行了解构,如下所示:

obj = {keyA: 'Hello from A', keyB: 'Hello from B'}
{keyA, keyB} = obj
Run Code Online (Sandbox Code Playgroud)

我在php中找到了这个list函数,它允许你像这样构造数组:

$info = array('coffee', 'brown', 'caffeine');
list($drink, $color, $power) = $info;
Run Code Online (Sandbox Code Playgroud)

有没有办法在PHP中解构对象或关联数组?如果不在核心库中,也许有人写了一些智能助手功能?

php destructuring

23
推荐指数
2
解决办法
1万
查看次数

ES6解构函数参数 - 命名根对象

有没有办法保留析构函数参数的名称?即,根对象的名称?

在ES5中,我可能会这样做(使用继承作为隐喻来说明问题):

// ES5:
var setupParentClass5 = function(options) {
    textEditor.setup(options.rows, options.columns);
};

var setupChildClass5 = function(options) {
    rangeSlider.setup(options.minVal, options.maxVal);
    setupParentClass5(options); // <= we pass the options object UP
};
Run Code Online (Sandbox Code Playgroud)

我使用相同的options对象来保存多个配置参数.一些参数由父类使用,一些参数由子类使用.

有没有办法在ES6中使用析构函数参数?

// ES6:
var setupParentClass6 = ({rows, columns}) => {
    textEditor.setup(rows, columns);
};

var setupChildClass6 = ({minVal, maxVal}) => {
    rangeSlider.setup(minVal, maxVal);
    setupParentClass6( /* ??? */ );  // how to pass the root options object?
};
Run Code Online (Sandbox Code Playgroud)

或者我是否需要提取所有选项setupChildClass6()以便可以单独传递setupParentClass6()

// ugh.
var setupChildClass6b = ({minVal, maxVal, …
Run Code Online (Sandbox Code Playgroud)

javascript arguments function destructuring ecmascript-6

21
推荐指数
3
解决办法
2647
查看次数

如何在ES2015中将所有属性解构为当前范围/闭包?

我想做这样的事情:

const vegetableColors = {corn: 'yellow', peas: 'green'};

const {*} = vegetableColors;

console.log(corn);// yellow
console.log(peas);// green
Run Code Online (Sandbox Code Playgroud)

我似乎无法找到或弄清楚如何做到这一点,但我真的以为我以前见过它!:P

注:我使用的是巴贝尔stage设置为0;

背景:我正试图在JSX中干燥而不是参考this.statethis.props在任何地方.如果数据发生变化,也不必继续为destructure添加属性.

javascript destructuring ecmascript-6 ecmascript-7

21
推荐指数
1
解决办法
4348
查看次数

将参数属性简写与TypeScript中的Destructuring结合使用

编辑

在TypeScript的Github repo上记录了一个问题,他们正在接受PR来实现它.


在TypeScript中,当我们想要从构造函数定义中自动在类中创建属性时,我们可以利用参数属性的简写,例如:

class Person {
    constructor(public firstName : string, public lastName : number, public age : number) {

    }
}
Run Code Online (Sandbox Code Playgroud)

然后,转换后的Javascript将是:

var Person = (function () {
    function Person(firstName, lastName, age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
    return Person;
})();
Run Code Online (Sandbox Code Playgroud)

但是如果我们想在构造函数中接收一个对象,它将是这样的:

interface IPerson {
    firstName : string,
    lastName : string,
    age: number
}

class Person {
    constructor(person : IPerson) {
        this.firstName = person.firstName;
        this.lastName = person.lastName;
        this.age = person.age; …
Run Code Online (Sandbox Code Playgroud)

destructuring typescript

21
推荐指数
2
解决办法
5911
查看次数

如何使用包含连字符的键将对象解构为变量?

如何从键包含连字符的对象中构造属性?

例如:

{
  accept-ranges:"bytes",
  cache-control:"public, max-age=0",
  content-length:"1174",
  content-type:"application/json",
  date:"Mon, 03 Oct 2016 06:45:03 GMT",
  etag:"W/"496-157892e555b"",
  last-modified:"Mon, 03 Oct 2016 06:14:57 GMT",
  x-powered-by:"Express"
}
Run Code Online (Sandbox Code Playgroud)

现在使用解构来从对象获取content-typex-powered-by值?

javascript destructuring ecmascript-6

21
推荐指数
1
解决办法
4182
查看次数