Loading ...
Sorry, an error occurred while loading the content.

Re: best practice for "making functions known" from other files

Expand Messages
  • Douglas Crockford
    ... I use /*global / to do that. It is far from ideal, but the language does not provide an ideal solution.
    Message 1 of 5 , Aug 24, 2008
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "DoNotSay" <arminx@...> wrote:
      >
      > I have structured my Javascript into to files
      >
      > basicfunctions.js
      > specialfunctions.js
      >
      >
      > "basicfunctions.js" gets included allways - as every page of my site
      > needs something from it
      >
      > "specialfunctions.js" only gets included on some special pages, as the
      > functions are only required there.
      >
      > "basicfunctions.js" also includes stuff like "myajaxcall", which I
      > reuse in "specialfunctions.js"
      >
      > NOW ... when jslinting specialfunctions.js, jslint complies, that
      > "myajaxcall" is not defined. Correct. Because "myajaxcall" (and around
      > 12 more basic functions) are defined in the other file.
      >
      > ATM I am declaring all those functions as globals within
      > specialfunctions.js
      >
      > Is this the best way, or is there something more elegant?

      I use /*global / to do that. It is far from ideal, but the language
      does not provide an ideal solution.
    • Paul
      ... I had a hellava time figuring this one out. What I did was throw all of my common functions into a namespace (eg. Common). So for example: Common =
      Message 2 of 5 , Aug 27, 2008
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "DoNotSay" <arminx@...> wrote:
        >
        > I have structured my Javascript into to files
        >
        > basicfunctions.js
        > specialfunctions.js
        >
        >
        > "basicfunctions.js" gets included allways - as every page of my site
        > needs something from it
        >
        > "specialfunctions.js" only gets included on some special pages, as the
        > functions are only required there.
        >
        > "basicfunctions.js" also includes stuff like "myajaxcall", which I
        > reuse in "specialfunctions.js"
        >
        > NOW ... when jslinting specialfunctions.js, jslint complies, that
        > "myajaxcall" is not defined. Correct. Because "myajaxcall" (and around
        > 12 more basic functions) are defined in the other file.
        >
        > ATM I am declaring all those functions as globals within
        > specialfunctions.js
        >
        > Is this the best way, or is there something more elegant?
        >
        > best wishes,
        >
        > Harald
        >

        I had a hellava time figuring this one out. What I did was throw all
        of my common functions into a namespace (eg. Common). So for example:

        Common = function () {
        functionX = function () {
        ...
        },

        myajaxcall = function () {
        ...
        }
        }();

        Then you can just refer to these functions as Common.myajaxcall(). On
        your other pages, you just put on the top /*global Common */ and that
        will tell jslint that Common is an accepted global object. Think of it
        as a "class" with "static functions".

        Hope that helps.
      • Paul
        ... Whoops, omit the parenthesis after the last brace. That assume a singleton. That isn t needed in this situation.
        Message 3 of 5 , Aug 27, 2008
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Paul" <DWTebriel@...> wrote:
          >
          > --- In jslint_com@yahoogroups.com, "DoNotSay" <arminx@> wrote:
          > >
          > > I have structured my Javascript into to files
          > >
          > > basicfunctions.js
          > > specialfunctions.js
          > >
          > >
          > > "basicfunctions.js" gets included allways - as every page of my site
          > > needs something from it
          > >
          > > "specialfunctions.js" only gets included on some special pages, as the
          > > functions are only required there.
          > >
          > > "basicfunctions.js" also includes stuff like "myajaxcall", which I
          > > reuse in "specialfunctions.js"
          > >
          > > NOW ... when jslinting specialfunctions.js, jslint complies, that
          > > "myajaxcall" is not defined. Correct. Because "myajaxcall" (and around
          > > 12 more basic functions) are defined in the other file.
          > >
          > > ATM I am declaring all those functions as globals within
          > > specialfunctions.js
          > >
          > > Is this the best way, or is there something more elegant?
          > >
          > > best wishes,
          > >
          > > Harald
          > >
          >
          > I had a hellava time figuring this one out. What I did was throw all
          > of my common functions into a namespace (eg. Common). So for example:
          >
          > Common = function () {
          > functionX = function () {
          > ...
          > },
          >
          > myajaxcall = function () {
          > ...
          > }
          > }();
          >
          > Then you can just refer to these functions as Common.myajaxcall(). On
          > your other pages, you just put on the top /*global Common */ and that
          > will tell jslint that Common is an accepted global object. Think of it
          > as a "class" with "static functions".
          >
          > Hope that helps.
          >

          Whoops, omit the parenthesis after the last brace. That assume a
          singleton. That isn't needed in this situation.
        • Douglas Crockford
          ... I think you meant either var Common = { functionX: function () {}, myajaxcall: function () {} }; or var Common = function () { return { functionX: function
          Message 4 of 5 , Aug 27, 2008
          • 0 Attachment
            > I had a hellava time figuring this one out. What I did was throw all
            > of my common functions into a namespace (eg. Common). So for example:
            >
            > Common = function () {
            > functionX = function () {
            > ...
            > },
            >
            > myajaxcall = function () {
            > ...
            > }
            > }();
            >
            > Then you can just refer to these functions as Common.myajaxcall(). On
            > your other pages, you just put on the top /*global Common */ and that
            > will tell jslint that Common is an accepted global object.

            I think you meant either

            var Common = {
            functionX: function () {},
            myajaxcall: function () {}
            };

            or

            var Common = function () {
            return {
            functionX: function () {},
            myajaxcall: function () {}
            };
            }();
          Your message has been successfully submitted and would be delivered to recipients shortly.