Last active
December 11, 2015 03:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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