redux - Javascript objects keys without value - Stack Overflow

I am following the redux tutorialand having a hard time understanding the following codeexport functi

I am following the redux tutorial and having a hard time understanding the following code

export function addTodo(text) {
      return { type: ADD_TODO, text }
    }

So the function above returns the object { type: ADD_TODO, text } and there are two things that confuse me.

  1. what is the value of this object associated with the key text. If this value is undefined, then why not just return { type: ADD_TODO} instead.

  2. If text is a string, then shouldn't it be { type: ADD_TODO, [text]: *some value* } instead?

Moreover, there are other functions like

function toggleTodo(index) {
      return { type: TOGGLE_TODO, index }
    }
function setVisibilityFilter(filter) {
      return { type: SET_VISIBILITY_FILTER, filter }
    }

Can someone explain this syntax to me?

I am following the redux tutorial https://redux.js/basics/actions and having a hard time understanding the following code

export function addTodo(text) {
      return { type: ADD_TODO, text }
    }

So the function above returns the object { type: ADD_TODO, text } and there are two things that confuse me.

  1. what is the value of this object associated with the key text. If this value is undefined, then why not just return { type: ADD_TODO} instead.

  2. If text is a string, then shouldn't it be { type: ADD_TODO, [text]: *some value* } instead?

Moreover, there are other functions like

function toggleTodo(index) {
      return { type: TOGGLE_TODO, index }
    }
function setVisibilityFilter(filter) {
      return { type: SET_VISIBILITY_FILTER, filter }
    }

Can someone explain this syntax to me?

Share Improve this question asked Dec 24, 2019 at 2:37 PhantomPhantom 4431 gold badge6 silver badges14 bronze badges 1
  • { type: ADD_TODO, text } is the es6 shorthand for { type: ADD_TODO, text:text } given a variable text. – Mark Commented Dec 24, 2019 at 2:39
Add a ment  | 

1 Answer 1

Reset to default 8

They're using ES6 Shorthand property names - If the intended key name is the same as the variable, then you can simply pass the variable

let name = 'Jared';
let age = 19;
let literate = false;

let obj = {
  name,
  age,
  literate
}

/* Is the same as...
let obj = {
  'name': name,
  'age': age,
  'literate': literate
}
*/

console.log(obj);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744783584a4593487.html

相关推荐

  • redux - Javascript objects keys without value - Stack Overflow

    I am following the redux tutorialand having a hard time understanding the following codeexport functi

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信