0

I'm trying to pass menu: "#menu" to the anonymous object using chWidth().

How can i do this?

$('#fullpage').fullpage({verticalCentered: false}, chWidth());


    function chWidth() {
        if ($(window).width()) {
            return {menu: "#menu"}
        }
    }

so it should ends up like this:
{ verticalCentered: false, menu: "#menu" }

How should the return statement be? I get an error if i try return menu:"#menu"

Denis Tsoi
  • 9,428
  • 8
  • 37
  • 56
Hyrule
  • 635
  • 2
  • 10
  • 23

2 Answers2

0

Your return statement is fine as is.

If you're using jQuery you can use $.extend like this:

$('#fullpage').fullpage($.extend({verticalCentered: false}, chWidth()));

Alternatively you can use Object.assign like this (but you'll lose support on older browsers):

$('#fullpage').fullpage(Object.assign({verticalCentered: false}, chWidth()));
winhowes
  • 7,845
  • 5
  • 28
  • 39
  • Should you not mark it as dupe instead? – Rajesh Mar 02 '17 at 10:09
  • @Rajesh I didn't do a check for dupes - didn't know if it was or wasn't. Figured the 20 seconds it took to add an answer was just as easy as searching for duplicates. – winhowes Mar 02 '17 at 10:10
0

First, your chWidth has incosistent return points.
You should return same type(object) in else branch.

To combine objects, you can to use Object.assign method:

function chWidth(cond) {
  // use cond for emulate both boolean cases
  if (cond) {
      return {menu: "#menu"};
  } else {
    return {};
  }
}

console.log(Object.assign({verticalCentered: false}, chWidth()));
console.log(Object.assign({verticalCentered: false}, chWidth(true)));
vp_arth
  • 14,461
  • 4
  • 37
  • 66