2

This might seem very barbaric; I am trying to pass fApp and sApp and call them as objects and be seen as the jquery objects. So fApp will just fit into place here: $(ele).obj[fApp](to); and be seen as .insertAfter. They do not even need to be objects; I just want a way so I can pass values instead of writing 1000 appends, prepends and other stuff.

explained again

I know, I don't know how else to explain it. Pretty much just taking " fApp " and setting the value as " prependTo " and then call it here " $(ele).obj[fApp](to); " and have it act as though it is an actual prependTo

This whole function is just my mobile appender and desktop reappender. The value from is what grabs the parent element and than knows to append, prepend, after or before it when changed to desktop view.

function appendMaster(ele, to, fApp, sApp) {
    var from = $(ele).parent();
    console.log(fApp);

    var obj = new Object;
    var obj2 = new Object;
    obj[fApp] = fApp;
    obj2[sApp] = sApp;

    var width = $(window).width();
    if (width < 640) {
        $(ele).obj[fApp](to);
    };
    if (width > 640 && width < 966) {
        // Do something
    };
    if (width > 966) {
        $(ele).obj2[sApp](from);
    };
}

appendMaster("#forming","body", "prependTo", "prependTo");
4
  • 1
    It's really hard to tell what it is you're talking about here. Commented Sep 11, 2013 at 12:34
  • I know, I don't know how else to explain it. Pretty much just taking " fApp " and setting the value as " prependTo " and then call it here " $(ele).obj[fApp](to); " and have it act as though it is an actual prependTo Commented Sep 11, 2013 at 12:38
  • 2
    What is "obj" supposed to be? I think that should probably be $(ele)[fApp](to) - the terminology you're looking for is "computed property name" Commented Sep 11, 2013 at 12:44
  • Yup, that made it work! Thanks pointy. Commented Sep 11, 2013 at 12:59

1 Answer 1

4

You can address properties in two ways in JS:

  • obj.prop
  • obj['prop']

Instead of 'prop' in the string notation above, you can put a variable, in your case fApp.

function appendMaster(ele, to, fApp, sApp) {
  var from = $(ele).parent();
  console.log(fApp);

  var width = $(window).width();
  if (width < 640) {
      $(ele)[fApp](to);
  };
  if (width > 640 && width < 966) {
      // Do something
  };
  if (width > 966) {
      $(ele)[sApp](from);
  };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.