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

How to return value in a function with a single line in Javascript? - Stack Overflow

programmeradmin9浏览0评论

This works:

var power = (x)=>x*x

This works as well: (this returns an object with a qty:1 property)

let addQty = (payload)=>{payload.qty=1;return payload}

But this doesn't:

let addQty2 = (payload)=>{return payload.qty=1}

It instead returns 1. Why?

ps: I know I can return the value after the statement like this:

let addQty2 = (payload)=>{payload.qty=1;return payload;}

,but I still can't understand why the previous one doesn't work.

This works:

var power = (x)=>x*x

This works as well: (this returns an object with a qty:1 property)

let addQty = (payload)=>{payload.qty=1;return payload}

But this doesn't:

let addQty2 = (payload)=>{return payload.qty=1}

It instead returns 1. Why?

ps: I know I can return the value after the statement like this:

let addQty2 = (payload)=>{payload.qty=1;return payload;}

,but I still can't understand why the previous one doesn't work.

Share Improve this question edited Jan 12, 2018 at 3:59 Akhmad Agosto asked Jan 12, 2018 at 3:50 Akhmad AgostoAkhmad Agosto 531 silver badge6 bronze badges 5
  • 4 In the second example, you are returning payload, in the third, you are returning payload.qty, which you have set to 1. – user3483203 Commented Jan 12, 2018 at 3:56
  • 1 jsfiddle/yak613/bgxpatgs. This returns payload. – yaakov Commented Jan 12, 2018 at 3:56
  • @TricksfortheWeb doesn't that return an exact {qty:1}? What I want is basically this jsfiddle/bgxpatgs/1, but by directly returning the value while mutating the object (if that is even possible) – Akhmad Agosto Commented Jan 12, 2018 at 4:04
  • Why don't you want to do option 2? It's very clear what you're trying to do and not too verbose. – jhpratt Commented Jan 12, 2018 at 4:06
  • @jhpratt I am sorry I didn't make myself clearer in the OP. I'm learning functional programming and should have used Object.assign to retain immutability. – Akhmad Agosto Commented Jan 12, 2018 at 7:41
Add a ment  | 

2 Answers 2

Reset to default 4

An assignment in JavaScript returns the assigned value. For example,

a = b = 1;

is parsed as

a = (b = 1);

The value of the expression of b = 1 is 1, so we assign that to a. Likewise, the return in your second example is parsed as

return (payload.qty = 1)

The value of the expression payload.qty = 1 is 1, so that is what is returned.

This code works, but realize that you are changing the original object and then returning it.

let addQty = payload=>{payload.qty=1; return payload;}

var obj = {
  name: "Some thing",
  price: 9.95
}

var newObj = addQty(obj);

console.log('obj', obj);
console.log('newObj', newObj);

This version:

let addQty2 = (payload)=>{return payload.qty=1}

Will return the last value used and not the object because the object is not the last value. the number 1 is the last value used.

If you want to only affect a new object then try this:

let addQty = payload=>Object.assign({}, payload, {qty:1});

var obj = {
  name: "Some thing",
  price: 9.95
}

var newObj = addQty(obj);

console.log('obj', obj);
console.log('newObj', newObj);

This function returns the value that is returned from Object.assign which is the new object.

发布评论

评论列表(0)

  1. 暂无评论