I ignored javascript forever. I started using jQuery a few years ago so that I can get through. But since I started making TDD more, I decided yesterday to really immerse myself in javascript (and maybe coffeescript after that).
I have many pages in my ASP.NET Web Forms application, and most of these pages currently do not have a ton of javascript. I am in the process of change. I use Jasmine with Chutzpah to create my tests.
I moved along with passing the tests and errors as expected. But then I wanted to create a namespace so that I did not stomp around the entire global space.
After reading this article:
http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-1/
I decided to try using the template from the article "Self-Employed Anonymous Function: Part 2 (Public and Private)". He seems to have the most flexibility and seems to encapsulate things very well.
I have a folder called / Scripts. In this folder are some of the frameworks that I use as jQuery, jasmine, (twitter) bootstrap and modernizr. I also have a subfolder called / Site, where I put my site code in multiple page-based files. (product.js, billing.js, etc.)
In the section / Scripts / site, I added a subfolder / Tests (or Specs), in which there are files (product_test.js, billing_tests.js, etc.).
. utility.js, padLeft. padLeft .js . , . // .js :
(function (myns, $, undefined) {
var isSomething = true;
myns.something = "something";
myns.padLeft = function (str, len, pad) {
str = Array(len + 1 - str.length).join(pad) + str;
return str;
};
}(window.myns= window.myns|| {}, jQuery ));
///utility_test.js
describe("Namespace myns with public property something", function () {
it("Should Equal 'something'", function () {
expect(myns.something).toEqual('something');
});
});
, myns.something "-".
. undefined.
, javascript ?
, , , . , , , - .
, , .
UPDATE: SOLVED
. @T.J. Crowder. , jsbin , , , , , , . , - .
. , , , . , , jQuery, , , jQuery. , - myns .
. , - . , , jQuery. - jQuery $ .