VS 2010 Intellisense and (function(){}()) wrapper
VS 2010 intellisense will not expose any constructors, properties or methods if the entire module is wrapped in (function(){}());
Define namespaces, and objects in the namespaces, out in the open. The only global will be the namespace definition, and all objects defined in them will be in that scope and not the global namespace scope.
This does not pollute the global namespace.
Consider:
/* global JsTest */
var JsTest = JsTest || {};
JsTest.Objects = JsTest.Objects || {};
JsTest.Objects.Rectangle = function (w, h) {
"use strict";
this.width = w;
this.height = h;
};
JsTest.Objects.Rectangle.prototype.area = function () {
"use strict"; return this.width * this.height;
};
JsTest.Objects.Rectangle.prototype.circumference = function () {
"use strict"; return ((2 * this.width) + (2 * this.height));
};
This code sample defines an object called Rectangle in the namespace JsTest.Objects.
Only JsTest is visible in the global namespace.
To make use of our Rectangle object, we call it as such:
<script src="JsTest.js"></script>
<script type="text/javascript">
var rect1 = new JsTest.Objects.Rectangle(3, 5);</p>
</script>
VS 2010 Intellisense will pickup on our loaded module’s object and it’s properties and methods.
Within a function wrapper, Intellisense works fine. It observes the scope of the variables. It will display them if they are in scope.
Being aware of this will help with coding when the definitions are in one module, and the use of them in another file that includes the module.
Leave a comment