最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - es6 hash array index function call mixed syntax - Stack Overflow

programmeradmin3浏览0评论

What kind of ES6 syntax is this?

{
  [ActionTypes.Repo](state, { username, res }) {
    /* ... */
  },

  [ActionTypes.Repo2](state, { username, res }) {
    /* ... */
}

Taken from : .js

What kind of ES6 syntax is this?

{
  [ActionTypes.Repo](state, { username, res }) {
    /* ... */
  },

  [ActionTypes.Repo2](state, { username, res }) {
    /* ... */
}

Taken from : https://github./quangbuule/redux-example/blob/master/src/js/reducers/Repo.js

Share Improve this question edited Dec 9, 2015 at 11:34 Francisc 80.5k65 gold badges185 silver badges278 bronze badges asked Jul 13, 2015 at 11:44 eguneyseguneys 6,3967 gold badges34 silver badges81 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 24

Those are method definitions, puted property names and destructuring at work.

Method definitions provide a concise way to create properties that contain functions:

// before
var obj = {
  foo: function() {}
};

// now
var obj = {
   foo() {}
};

That's the same syntax for creating methods in class definitions.

Computed properties allow you to use the result of any expression as property name in an object literal:

var foo='somePropertyName';

// before
var obj = {};
obj[foo] = 42;

// now

var obj = {
  [foo]: 42
};

And of course this also works with method definitions:

var obj = {
  [foo]() {}
};

Destructuring is like pattern matching and makes it easier to refer to nested properties of an array/object if thats all you need:

// before
function foo(obj) {
  var username = obj.username;
  var res = obj.res;
}

// now
function foo({username, res}) {

}
发布评论

评论列表(0)

  1. 暂无评论