Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Code I don't understand.

Code I don't understand.

Scheduled Pinned Locked Moved JavaScript
javascriptquestion
7 Posts 6 Posters 0 Views 1 Watching
  • 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.
  • C Offline
    C Offline
    ChicagoBobT
    wrote on last edited by
    #1

    I am looking over some code that someone else wrote for three.js The main.js file starts out with this.

    var APP = {};

    (function () {
    "use strict";

    /*global VT,THREE,Detector,requestAnimationFrame*/
    /*jslint browser: true*/

    /***************************************************************************
    * Global Variables
    */

    // three.js variables
    var scene = null;
    var renderer = null;
    var camera = null;
    var controls = null;
    var mesh = null;
    var clock = new THREE.Clock();

    var domContainer = null;
    var virtualTexture = null;

    /***************************************************************************
    * Initialiaze application
    */

    function resize() {
    renderer.setSize(window.innerWidth, window.innerHeight);
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    }

    .
    .
    .
    APP.load = function (geometry, config) {

    // create virtual texture
    geometry.computeTangents();
    geometry.computeVertexNormals();
    
    virtualTexture = new THREE.VirtualTexture(renderer.context, config);
    var material = THREE.createVirtualTextureMaterial(virtualTexture);
    
    mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);
    
    THREE.duplicateGeometryForVirtualTexturing(geometry, virtualTexture);
    

    };

    }());

    and the code ends like above. I dont get this line (function () { How does that work or even get called? The code actually works and not seeing anything like this before just made me curious. Hope someone can shed some light on this. Thanks, Bob

    G A S 3 Replies Last reply
    0
    • C ChicagoBobT

      I am looking over some code that someone else wrote for three.js The main.js file starts out with this.

      var APP = {};

      (function () {
      "use strict";

      /*global VT,THREE,Detector,requestAnimationFrame*/
      /*jslint browser: true*/

      /***************************************************************************
      * Global Variables
      */

      // three.js variables
      var scene = null;
      var renderer = null;
      var camera = null;
      var controls = null;
      var mesh = null;
      var clock = new THREE.Clock();

      var domContainer = null;
      var virtualTexture = null;

      /***************************************************************************
      * Initialiaze application
      */

      function resize() {
      renderer.setSize(window.innerWidth, window.innerHeight);
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
      }

      .
      .
      .
      APP.load = function (geometry, config) {

      // create virtual texture
      geometry.computeTangents();
      geometry.computeVertexNormals();
      
      virtualTexture = new THREE.VirtualTexture(renderer.context, config);
      var material = THREE.createVirtualTextureMaterial(virtualTexture);
      
      mesh = new THREE.Mesh(geometry, material);
      scene.add(mesh);
      
      THREE.duplicateGeometryForVirtualTexturing(geometry, virtualTexture);
      

      };

      }());

      and the code ends like above. I dont get this line (function () { How does that work or even get called? The code actually works and not seeing anything like this before just made me curious. Hope someone can shed some light on this. Thanks, Bob

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      All of the code is encapsulated inside an anonymous function that starts with the

      (function () {

      line and ends with

      }());

      The function is executed straight away - the () on the last line calls it with no arguments. The only variable that this script exposes is "APP", which is an object containing the "load" function (and anything else that was added in the ... part).

      C S L 3 Replies Last reply
      0
      • G Graham Breach

        All of the code is encapsulated inside an anonymous function that starts with the

        (function () {

        line and ends with

        }());

        The function is executed straight away - the () on the last line calls it with no arguments. The only variable that this script exposes is "APP", which is an object containing the "load" function (and anything else that was added in the ... part).

        C Offline
        C Offline
        ChicagoBobT
        wrote on last edited by
        #3

        Thanks so much.

        1 Reply Last reply
        0
        • C ChicagoBobT

          I am looking over some code that someone else wrote for three.js The main.js file starts out with this.

          var APP = {};

          (function () {
          "use strict";

          /*global VT,THREE,Detector,requestAnimationFrame*/
          /*jslint browser: true*/

          /***************************************************************************
          * Global Variables
          */

          // three.js variables
          var scene = null;
          var renderer = null;
          var camera = null;
          var controls = null;
          var mesh = null;
          var clock = new THREE.Clock();

          var domContainer = null;
          var virtualTexture = null;

          /***************************************************************************
          * Initialiaze application
          */

          function resize() {
          renderer.setSize(window.innerWidth, window.innerHeight);
          camera.aspect = window.innerWidth / window.innerHeight;
          camera.updateProjectionMatrix();
          }

          .
          .
          .
          APP.load = function (geometry, config) {

          // create virtual texture
          geometry.computeTangents();
          geometry.computeVertexNormals();
          
          virtualTexture = new THREE.VirtualTexture(renderer.context, config);
          var material = THREE.createVirtualTextureMaterial(virtualTexture);
          
          mesh = new THREE.Mesh(geometry, material);
          scene.add(mesh);
          
          THREE.duplicateGeometryForVirtualTexturing(geometry, virtualTexture);
          

          };

          }());

          and the code ends like above. I dont get this line (function () { How does that work or even get called? The code actually works and not seeing anything like this before just made me curious. Hope someone can shed some light on this. Thanks, Bob

          A Offline
          A Offline
          Anlige
          wrote on last edited by
          #4

          (function () {
          /*TODO*/
          }());

          equal with:

          var fn = function(){
          /*TODO*/
          };
          fn();

          look at the following codes:

          (function(words){alert(words);}('hello!'));

          1 Reply Last reply
          0
          • G Graham Breach

            All of the code is encapsulated inside an anonymous function that starts with the

            (function () {

            line and ends with

            }());

            The function is executed straight away - the () on the last line calls it with no arguments. The only variable that this script exposes is "APP", which is an object containing the "load" function (and anything else that was added in the ... part).

            S Offline
            S Offline
            Sunasara Imdadhusen
            wrote on last edited by
            #5

            Really good explanation

            1 Reply Last reply
            0
            • C ChicagoBobT

              I am looking over some code that someone else wrote for three.js The main.js file starts out with this.

              var APP = {};

              (function () {
              "use strict";

              /*global VT,THREE,Detector,requestAnimationFrame*/
              /*jslint browser: true*/

              /***************************************************************************
              * Global Variables
              */

              // three.js variables
              var scene = null;
              var renderer = null;
              var camera = null;
              var controls = null;
              var mesh = null;
              var clock = new THREE.Clock();

              var domContainer = null;
              var virtualTexture = null;

              /***************************************************************************
              * Initialiaze application
              */

              function resize() {
              renderer.setSize(window.innerWidth, window.innerHeight);
              camera.aspect = window.innerWidth / window.innerHeight;
              camera.updateProjectionMatrix();
              }

              .
              .
              .
              APP.load = function (geometry, config) {

              // create virtual texture
              geometry.computeTangents();
              geometry.computeVertexNormals();
              
              virtualTexture = new THREE.VirtualTexture(renderer.context, config);
              var material = THREE.createVirtualTextureMaterial(virtualTexture);
              
              mesh = new THREE.Mesh(geometry, material);
              scene.add(mesh);
              
              THREE.duplicateGeometryForVirtualTexturing(geometry, virtualTexture);
              

              };

              }());

              and the code ends like above. I dont get this line (function () { How does that work or even get called? The code actually works and not seeing anything like this before just made me curious. Hope someone can shed some light on this. Thanks, Bob

              S Offline
              S Offline
              Suresh Amudalapalli
              wrote on last edited by
              #6

              It’s an Immediately-Invoked Function Expression, or shorter: IIFE. It executes immediately after it’s created. This pattern is often used when trying to avoid polluting the global namespace, because all the variables used in the function are not visible outside its scope.

              1 Reply Last reply
              0
              • G Graham Breach

                All of the code is encapsulated inside an anonymous function that starts with the

                (function () {

                line and ends with

                }());

                The function is executed straight away - the () on the last line calls it with no arguments. The only variable that this script exposes is "APP", which is an object containing the "load" function (and anything else that was added in the ... part).

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Nice explanation :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups