sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [JS] Global variables

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 189 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • jolranJ Offline
      jolran
      last edited by

      We all know that $ Global variables are bad in Ruby. And that you have to wrap your code inside a module, (doublewrapping is even better) not to let your code crap(expression from Dan πŸ˜„ ) on other peoples code.

      So on the JS side of things. When the dialog gets more complex there comes a point where one have to store an array or such outside of functions. Simply slapping arguments around wont work. (one can use hidden inputfields, but thats not the question here)

      I can imagine a lot of newcomers are not always aware that the var becomes global then. If I understand it right is the same as in ruby $ ?

      Read some here..

      favicon

      (www.rafilabs.com)

      So how do we deal with Webdialogs namespacing?
      Are there any special circumstances since we are inside Sketchup enviroment?
      Something like, global vars are not that harmful if kept at minimum?

      I see Thomthom is doing a namespacingfunction in TT_lib, so that got me thinking.

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        @jolran said:

        :circle: So how do we deal with Webdialogs namespacing?

        Same as regular JavaScript namespacing.

        @jolran said:

        :circle: Are there any special circumstances since we are inside Sketchup enviroment?

        No.

        @jolran said:

        :circle: Something like, global vars are not that harmful if kept at minimum?

        Globals should be avoided regardless.
        In the case of WebDialogs and JS, one WebDialog instance won't affect another instance. All is restricted to the scope of the HTML page loaded.

        Namespacing is still good when you want to use other libraries, you then want to ensure you do not conflict.

        I have to admit I'm a little bit more relaxed when it comes to JS in WebDialog, I often don't namespace them if I just make a simple dialog. (I loathe the JS syntax for namespacing - it's messy to say the least!) But as of TT_Lib, which is a library I have namespaced it because it's mean to be used in different environments.

        It is good practice to always namespace and isolate your code.

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • jolranJ Offline
          jolran
          last edited by

          Thank you Thomthom. I think you answered all my questions.

          @unknownuser said:

          All is restricted to the scope of the HTML page loaded

          This was my main concern. I don't want to pollute other people's plugins if I create a global var.

          @unknownuser said:

          Namespacing is still good when you want to use other libraries, you then want to ensure you do not conflict.

          Ah, I think I understand now. Like if you create a Jquery plugin for public release, for ex?

          @unknownuser said:

          But as of TT_Lib, which is a library I have namespaced it because it's mean to be used in different environments.

          A, yeah. I forgot for a minute it was a library when I was pooking around. Your library is a very useful resource for me, since you are using Jquery πŸ˜„ .

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement