Skip to content

Instantly share code, notes, and snippets.

@maxidr
Last active December 11, 2015 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxidr/4535365 to your computer and use it in GitHub Desktop.
Save maxidr/4535365 to your computer and use it in GitHub Desktop.
An example complete of module pattern in javascript. From: http://briancray.com/posts/javascript-module-pattern
<h1 id="qunit-header">Unit Tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"></div>​
var Person = (function () {
var foo = 'bar';
var firstName;
var lastName;
var Person = function(first, last){
firstName = first;
lastName = last;
};
Person.prototype = {
constructor: Person,
name: function(){
return lastName + ", " + firstName;
},
firstName: function() { return firstName },
lastName: function() { return lastName }
};
return Person;
})();
var john = new Person('John', 'Doe');
test("the function name() must be public (accessible from outside)", function() {
equal(john.name(), 'Doe, John');
});
test("the attribute foo must be private", function(){
equal(john.hasOwnProperty('foo'), false);
});
test("the attribute firstName must be private", function(){
equal(john.hasOwnProperty('firstName'), false);
});
test("the function firstName must be public", function(){
equal(john.firstName(), 'John');
});
name: Module pattern complete
description: http://briancray.com/posts/javascript-module-pattern
authors:
- Maxi Dello Russo
resources:
- http://code.jquery.com/qunit/qunit-1.10.0.js
- http://code.jquery.com/qunit/qunit-1.10.0.css
normalize_css: no
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment