Code I don't understand.
-
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
-
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
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).
-
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).
Thanks so much.
-
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
-
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).
Really good explanation
-
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
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.
-
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).