How can I set and access a global var in a prototype way?
var app;
(function(){
"use strict";
var App = function() {
};
App = App;
}(window));
$(function() {
app = new App();
});
When you're using strict mode, the value of this inside the IIFE isn't window, it's probably undefined, so App isn't really global.
If you explicitly make it global it should work
var app;
(function (w) {
"use strict";
w.App = function () {
};
}(window));
$(function () {
app = new App();
});
FIDDLE
If you weren't using strict mode, you could just remove the var keyword
Related
I have a JavaScript "class" that looks like this:
(function() {
'use strict';
function Calculator() {
this.currentValue = 0;
}
Calculator.prototype.add = true;
return Calculator;
}());
Now I am trying to test this with Jasmine – the CalculatorSpec.js looks like this:
(function() {
'use strict';
var calculator;
beforeEach(function() {
calculator = new Calculator();
});
describe('Calculator', function() {
it('should contain a function called "add"', function() {
expect(calculator.add).toBeTruthy();
});
});
})();
How am I supposed to access Calculator inside the Jasmine IIFE?
The files are included in the correct order in the specrunner, so I am sure it's a scope problem.
I already tried passing it to the IIFE as argument, but the problem is that Calculator is not available in global scope I guess.
You somehow have to make Calculator globally accessible to be able to test it. (And.. how would you otherwise use it in other code blocks?) The easiest way to do this by assigning the IIFE to a variable:
var Calculator = (function() {
'use strict';
function Calculator() {
this.currentValue = 0;
}
Calculator.prototype.add = true;
return Calculator;
}());
I see a lot of namespacing examples with functions but, is it o.k. to declare variables (global to my program) in this way?
var mynamespace = {};
mynamespace.var1 = 5;
or should all variables be placed in functions within the namespace?
You should avoid global variables...
Use some sort of module pattern instead, e.g.
(function () {
"use strict";
var myVar = 'blob';
}());
See http://yuiblog.com/blog/2007/06/12/module-pattern/
EDIT:
More Clarification:
var NS1 = NS1 || {};
NS1.myModule = function () {
"use strict";
var myVar = 'blob';
return {
myPublicMethod: function () {
return myVar;
}
};
}();
I'd like to "use strict"; mode in javascript but have some problems with strict warnings.
I have a "widget", e.g.:
var Widget = function () { /* ... */ }
Before I was using it like this (inside a module):
(function () {
var w1 = new Widget();
}());
when I add "use strict"; to this context I get a warning that "Widget" is not defined:
(function () {
"use strict";
var w1 = new Widget(); // <- warning here
}());
What is the proper way of doing this?
Should I define my "widgets" differently?
In strict mode you can't accidentally create global variables. Trying to do so will throw a reference error, as you've noticed. Hence you need to name an object, which has Widget assigned:
window.Widget = function () { /* ... */ }
(function () {
"use strict";
var w1 = new window.Widget();
}());
More info: MDN , www.nczonline.net
I have seen the following code
var MODULE = (function () {
var my = {},
privateVariable = 1;
function privateMethod() {
// ...
}
my.moduleProperty = 1;
my.moduleMethod = function () {
// ...
};
return my;
}());
the properties can be accessed like MODULE.moduleProperty ...right?
But how to access globals privateVariable and privateMethod() inside the module(which are globals insode the module ...right?)
You can only access them from WITHIN the module code itself as such:
var MODULE = (function () {
var my = {},
privateVariable = 1;
function privateMethod() {
alert('this is private!');
}
my.moduleProperty = 1;
my.moduleMethod = function () {
privateMethod();
return privateVariable;
};
return my;
}());
Doing this:
MODULE.moduleMethod();
Will call private method (and alert 'this is private!') and return the value of privateVariable.
There is no way to access privateVariable or privateMethod outside the MODULE scope.
var MODULE = (function() {
//...declare your module as above
}());
console.log(MODULE.privateVariable); //logs undefined
Hopefully that helps clear it up for you.
No, they are not global, they are local variables inside the anonymous function.
You can access them from any code within the function, but outside the function they are not directly accessible.
I have the following function
var myInstance = (function() {
var privateVar = 'Test';
function privateMethod () {
// ...
}
return { // public interface
publicMethod1: function () {
// all private members are accesible here
alert(privateVar);
},
publicMethod2: function () {
}
};
})();
what's the difference if I add a new to the function. From firebug, it seems two objects are the same. And as I understand, both should enforce the singleton pattern.
var myInstance = new (function() {
var privateVar = 'Test';
function privateMethod () {
// ...
}
return { // public interface
publicMethod1: function () {
// all private members are accesible here
alert(privateVar);
},
publicMethod2: function () {
}
};
})();
While the end result seems identical, how it got there and what it executed in is different.
The first version executes the anonymous function with this being in the context of the window object. The second version executes the anonymous function, but this is in the context of a new empty object.
In the end, they both return another object(your Singleton). It's just a slight difference in execution context.
To test this out, but an alert(this); right before the declaration of the privateVar variable.
#Tom Squires: That's not necessarily true and is poor practice not to declare your variables. A script with the "use strict"; directive does cause the JS engine to complain (assuming that the engine supports "use strict";