QUnit doesn't really care one way or the other. It's basically a wrapper that provides pretty output for assertions.
As for the difference between the two patterns, Pattern 1 (as you have implemented it) has all-public properties. You can do private properties, though:
function class () {
var propertyA = null;
this.methodA = function() {
alert('');
}
}
var object = new class();
object.methodA();
Pattern 2 won't actually work. You're just providing a result object wrapped in a closure, so you can't really call new on it. You would need to do it like this:
var class = (function() {
var propertyA = null;
return function () {
return {
methodA: function() {
alert('');
}
};
}
}());
var object = new class();
object.methodA();
The whole point of the new keyword is that it calls the object's constructor with a new variable bound to this. If you don't have a constructor, you're just creating a singleton.
For practical purposes, here's what's happening:
- In Pattern 1, a new
propertyA is created for each instance of class.
- In Pattern 2 (as you implemented it), you're creating a singleton object.
- In Pattern 2 (as I implemented it), a single
propertyA is shared among all instances (like static members in C++).
This is because in Pattern 1 you're creating a straightforward constructor function, which creates a new closure around the public methods for each invocation of new. Whereas in Pattern 2, you're creating a single closure around the constructor and declaring variables outside of the constructor's scope.