GSAP js function identifier fails when passing string as parameter in another function












1















I've been working with GSAP tutorial (links below) and wanted to make a function out of this



tl.from(img, 1, {y: -20, autoAlpha:0, ease: Bounce.easeOut})


so I've created this



    // a = selector, b = duration, c = axis, d = offset, e = type of animation, f= easeOut/In

// Problem occurs when passing c as either 'x'/'y' string

function test(a,b,c,d,e,f){
var tl=new TimelineLite;
var img = $('img');
var result = tl.from(a, b, c:d, ease:e+'.'+f});
return result;

}

test('img',2,"y",-370,"Bounce","easeOut");


As soon as this runs, it doesn't work and has 0 errors in console as shown here
http://weconnect.ro/stack/



If within the function I modify the c identifier to the y it runs like magic:
https://weconnect.ro/stack/working.html



I'm fairly new to JS and guess the problem might be that y is not really a string (and pretty sure that's not the best method to pass strings from function identifiers).



I'm also clueless on how I would console.log(this) as console.log(test('img',2,"y",-370,"Bounce","easeOut")); outputs the deconstruction of the TL function which I assume is its objects and that's probably the way to go around constructing it.



Any clarification on the matter would be greatly appreciated, thanks!










share|improve this question



























    1















    I've been working with GSAP tutorial (links below) and wanted to make a function out of this



    tl.from(img, 1, {y: -20, autoAlpha:0, ease: Bounce.easeOut})


    so I've created this



        // a = selector, b = duration, c = axis, d = offset, e = type of animation, f= easeOut/In

    // Problem occurs when passing c as either 'x'/'y' string

    function test(a,b,c,d,e,f){
    var tl=new TimelineLite;
    var img = $('img');
    var result = tl.from(a, b, c:d, ease:e+'.'+f});
    return result;

    }

    test('img',2,"y",-370,"Bounce","easeOut");


    As soon as this runs, it doesn't work and has 0 errors in console as shown here
    http://weconnect.ro/stack/



    If within the function I modify the c identifier to the y it runs like magic:
    https://weconnect.ro/stack/working.html



    I'm fairly new to JS and guess the problem might be that y is not really a string (and pretty sure that's not the best method to pass strings from function identifiers).



    I'm also clueless on how I would console.log(this) as console.log(test('img',2,"y",-370,"Bounce","easeOut")); outputs the deconstruction of the TL function which I assume is its objects and that's probably the way to go around constructing it.



    Any clarification on the matter would be greatly appreciated, thanks!










    share|improve this question

























      1












      1








      1








      I've been working with GSAP tutorial (links below) and wanted to make a function out of this



      tl.from(img, 1, {y: -20, autoAlpha:0, ease: Bounce.easeOut})


      so I've created this



          // a = selector, b = duration, c = axis, d = offset, e = type of animation, f= easeOut/In

      // Problem occurs when passing c as either 'x'/'y' string

      function test(a,b,c,d,e,f){
      var tl=new TimelineLite;
      var img = $('img');
      var result = tl.from(a, b, c:d, ease:e+'.'+f});
      return result;

      }

      test('img',2,"y",-370,"Bounce","easeOut");


      As soon as this runs, it doesn't work and has 0 errors in console as shown here
      http://weconnect.ro/stack/



      If within the function I modify the c identifier to the y it runs like magic:
      https://weconnect.ro/stack/working.html



      I'm fairly new to JS and guess the problem might be that y is not really a string (and pretty sure that's not the best method to pass strings from function identifiers).



      I'm also clueless on how I would console.log(this) as console.log(test('img',2,"y",-370,"Bounce","easeOut")); outputs the deconstruction of the TL function which I assume is its objects and that's probably the way to go around constructing it.



      Any clarification on the matter would be greatly appreciated, thanks!










      share|improve this question














      I've been working with GSAP tutorial (links below) and wanted to make a function out of this



      tl.from(img, 1, {y: -20, autoAlpha:0, ease: Bounce.easeOut})


      so I've created this



          // a = selector, b = duration, c = axis, d = offset, e = type of animation, f= easeOut/In

      // Problem occurs when passing c as either 'x'/'y' string

      function test(a,b,c,d,e,f){
      var tl=new TimelineLite;
      var img = $('img');
      var result = tl.from(a, b, c:d, ease:e+'.'+f});
      return result;

      }

      test('img',2,"y",-370,"Bounce","easeOut");


      As soon as this runs, it doesn't work and has 0 errors in console as shown here
      http://weconnect.ro/stack/



      If within the function I modify the c identifier to the y it runs like magic:
      https://weconnect.ro/stack/working.html



      I'm fairly new to JS and guess the problem might be that y is not really a string (and pretty sure that's not the best method to pass strings from function identifiers).



      I'm also clueless on how I would console.log(this) as console.log(test('img',2,"y",-370,"Bounce","easeOut")); outputs the deconstruction of the TL function which I assume is its objects and that's probably the way to go around constructing it.



      Any clarification on the matter would be greatly appreciated, thanks!







      javascript gsap






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 8:01









      David JSDavid JS

      82




      82
























          1 Answer
          1






          active

          oldest

          votes


















          0














          This is a bit of a complex problem that requires a bit of explanation of some more confusing parts of the JavaScript Language. I'll try to break this down a bit.



          The function tl.from(img, 1, {y: -20, autoAlpha:0, ease: Bounce.easeOut}) is taking an object (everything inside the {}) as the 3rd parameter. In the format that you have written it, the object is an anonymous object which is created on the fly in the method call. However, you could explicitly create the object, and pass the object as the parameter.



          i.e. you could call the function as tl.from(img, 1, objectWithVariousProperties)



          This leads to the next issue, which is that you are trying to pass a string to your function that represents the name of a particular property. To be able to access the property in this manner, you would use the JavaScript Object Bracket Notation, rather than Dot Notation. That looks something like objectWithVariousProperties[someVariableNamingAProperty].



          So, putting this together, you would take in your parameters, create an object and prepare it's properties, then pass the object to the function. Something like the following:



          Note I changed the variable names to make the code as clear as possible, and am showing 3 different methods for assigning a value to an Object Property.



          function test(selector, duration, axis, offset, animation, fadeDirection){

          var tl=new TimelineLite;
          var img = $(selector);

          var propertyObject = {
          autoAlpha: 0 // inline property assignment
          };
          propertyObject[axis] = offset; //bracket notation
          propertyObject.ease = animation + '.' + fadeDirection; //dot notation

          var result = tl.from(img, duration, propertyObject);
          return result;
          }





          share|improve this answer
























          • Thank you for the insightful comment, I've thought that in the creation of said object was mandatory, felt that way. I'll re-read once I get home and use the information. It's way clearer now, thank you again!

            – David JS
            Nov 25 '18 at 12:52











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465681%2fgsap-js-function-identifier-fails-when-passing-string-as-parameter-in-another-fu%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          This is a bit of a complex problem that requires a bit of explanation of some more confusing parts of the JavaScript Language. I'll try to break this down a bit.



          The function tl.from(img, 1, {y: -20, autoAlpha:0, ease: Bounce.easeOut}) is taking an object (everything inside the {}) as the 3rd parameter. In the format that you have written it, the object is an anonymous object which is created on the fly in the method call. However, you could explicitly create the object, and pass the object as the parameter.



          i.e. you could call the function as tl.from(img, 1, objectWithVariousProperties)



          This leads to the next issue, which is that you are trying to pass a string to your function that represents the name of a particular property. To be able to access the property in this manner, you would use the JavaScript Object Bracket Notation, rather than Dot Notation. That looks something like objectWithVariousProperties[someVariableNamingAProperty].



          So, putting this together, you would take in your parameters, create an object and prepare it's properties, then pass the object to the function. Something like the following:



          Note I changed the variable names to make the code as clear as possible, and am showing 3 different methods for assigning a value to an Object Property.



          function test(selector, duration, axis, offset, animation, fadeDirection){

          var tl=new TimelineLite;
          var img = $(selector);

          var propertyObject = {
          autoAlpha: 0 // inline property assignment
          };
          propertyObject[axis] = offset; //bracket notation
          propertyObject.ease = animation + '.' + fadeDirection; //dot notation

          var result = tl.from(img, duration, propertyObject);
          return result;
          }





          share|improve this answer
























          • Thank you for the insightful comment, I've thought that in the creation of said object was mandatory, felt that way. I'll re-read once I get home and use the information. It's way clearer now, thank you again!

            – David JS
            Nov 25 '18 at 12:52
















          0














          This is a bit of a complex problem that requires a bit of explanation of some more confusing parts of the JavaScript Language. I'll try to break this down a bit.



          The function tl.from(img, 1, {y: -20, autoAlpha:0, ease: Bounce.easeOut}) is taking an object (everything inside the {}) as the 3rd parameter. In the format that you have written it, the object is an anonymous object which is created on the fly in the method call. However, you could explicitly create the object, and pass the object as the parameter.



          i.e. you could call the function as tl.from(img, 1, objectWithVariousProperties)



          This leads to the next issue, which is that you are trying to pass a string to your function that represents the name of a particular property. To be able to access the property in this manner, you would use the JavaScript Object Bracket Notation, rather than Dot Notation. That looks something like objectWithVariousProperties[someVariableNamingAProperty].



          So, putting this together, you would take in your parameters, create an object and prepare it's properties, then pass the object to the function. Something like the following:



          Note I changed the variable names to make the code as clear as possible, and am showing 3 different methods for assigning a value to an Object Property.



          function test(selector, duration, axis, offset, animation, fadeDirection){

          var tl=new TimelineLite;
          var img = $(selector);

          var propertyObject = {
          autoAlpha: 0 // inline property assignment
          };
          propertyObject[axis] = offset; //bracket notation
          propertyObject.ease = animation + '.' + fadeDirection; //dot notation

          var result = tl.from(img, duration, propertyObject);
          return result;
          }





          share|improve this answer
























          • Thank you for the insightful comment, I've thought that in the creation of said object was mandatory, felt that way. I'll re-read once I get home and use the information. It's way clearer now, thank you again!

            – David JS
            Nov 25 '18 at 12:52














          0












          0








          0







          This is a bit of a complex problem that requires a bit of explanation of some more confusing parts of the JavaScript Language. I'll try to break this down a bit.



          The function tl.from(img, 1, {y: -20, autoAlpha:0, ease: Bounce.easeOut}) is taking an object (everything inside the {}) as the 3rd parameter. In the format that you have written it, the object is an anonymous object which is created on the fly in the method call. However, you could explicitly create the object, and pass the object as the parameter.



          i.e. you could call the function as tl.from(img, 1, objectWithVariousProperties)



          This leads to the next issue, which is that you are trying to pass a string to your function that represents the name of a particular property. To be able to access the property in this manner, you would use the JavaScript Object Bracket Notation, rather than Dot Notation. That looks something like objectWithVariousProperties[someVariableNamingAProperty].



          So, putting this together, you would take in your parameters, create an object and prepare it's properties, then pass the object to the function. Something like the following:



          Note I changed the variable names to make the code as clear as possible, and am showing 3 different methods for assigning a value to an Object Property.



          function test(selector, duration, axis, offset, animation, fadeDirection){

          var tl=new TimelineLite;
          var img = $(selector);

          var propertyObject = {
          autoAlpha: 0 // inline property assignment
          };
          propertyObject[axis] = offset; //bracket notation
          propertyObject.ease = animation + '.' + fadeDirection; //dot notation

          var result = tl.from(img, duration, propertyObject);
          return result;
          }





          share|improve this answer













          This is a bit of a complex problem that requires a bit of explanation of some more confusing parts of the JavaScript Language. I'll try to break this down a bit.



          The function tl.from(img, 1, {y: -20, autoAlpha:0, ease: Bounce.easeOut}) is taking an object (everything inside the {}) as the 3rd parameter. In the format that you have written it, the object is an anonymous object which is created on the fly in the method call. However, you could explicitly create the object, and pass the object as the parameter.



          i.e. you could call the function as tl.from(img, 1, objectWithVariousProperties)



          This leads to the next issue, which is that you are trying to pass a string to your function that represents the name of a particular property. To be able to access the property in this manner, you would use the JavaScript Object Bracket Notation, rather than Dot Notation. That looks something like objectWithVariousProperties[someVariableNamingAProperty].



          So, putting this together, you would take in your parameters, create an object and prepare it's properties, then pass the object to the function. Something like the following:



          Note I changed the variable names to make the code as clear as possible, and am showing 3 different methods for assigning a value to an Object Property.



          function test(selector, duration, axis, offset, animation, fadeDirection){

          var tl=new TimelineLite;
          var img = $(selector);

          var propertyObject = {
          autoAlpha: 0 // inline property assignment
          };
          propertyObject[axis] = offset; //bracket notation
          propertyObject.ease = animation + '.' + fadeDirection; //dot notation

          var result = tl.from(img, duration, propertyObject);
          return result;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 9:06









          ClaiesClaies

          19.9k33466




          19.9k33466













          • Thank you for the insightful comment, I've thought that in the creation of said object was mandatory, felt that way. I'll re-read once I get home and use the information. It's way clearer now, thank you again!

            – David JS
            Nov 25 '18 at 12:52



















          • Thank you for the insightful comment, I've thought that in the creation of said object was mandatory, felt that way. I'll re-read once I get home and use the information. It's way clearer now, thank you again!

            – David JS
            Nov 25 '18 at 12:52

















          Thank you for the insightful comment, I've thought that in the creation of said object was mandatory, felt that way. I'll re-read once I get home and use the information. It's way clearer now, thank you again!

          – David JS
          Nov 25 '18 at 12:52





          Thank you for the insightful comment, I've thought that in the creation of said object was mandatory, felt that way. I'll re-read once I get home and use the information. It's way clearer now, thank you again!

          – David JS
          Nov 25 '18 at 12:52




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465681%2fgsap-js-function-identifier-fails-when-passing-string-as-parameter-in-another-fu%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen