-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make the new
operator optional before the JSZip
constructor
#93
Conversation
This line of code implements John Resig's self-calling constructor pattern. See also: http://ejohn.org/blog/simple-class-instantiation/ http://habrahabr.ru/post/135027/
@Mithgol Nice ! Could you add an unit test for this constructor ? We already have one for current constructor. And I agree with you, this leads to a new minor version. @Stuk should I prepare a v2.1.1 (with only #91) first / in parallel ? I think #91 is a bit urgent (as the installation fails behind a restrictive firewall) but we could wait to have more stuff for the v2.2.0. What do you think of it ? |
@dduponchel a quick v2.1.1 with #91 sounds good to me |
I'm preparing it ! |
I can see the version 2.1.1 in npm registry. I have tested it, it works great! |
I've just added a test; it passed on Travis CI. |
@@ -59,6 +59,9 @@ test("JSZip", function(){ | |||
|
|||
var zip = new JSZip(); | |||
ok(zip, "Constructor works"); | |||
|
|||
var zipNoNew = JSZip(); | |||
ok(zipNoNew.file, "Constructor adds `new` before itself where necessary"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these tests would be more explicit if changed to ok(zip instanceof JSZip, ...)
. Would you mind doing that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
make the `new` operator optional before the `JSZip` constructor
See Stuk#93 for more informations.
As @qlqllu has reported, an attempt to shorten JSZip call (giving it the form
var jsZIP = new require('jszip')()
) fails.The failure's cause is a well-known JavaScript's operator precedence (the operator
new
has higher precedence than a function call — even if Node'srequire
is that function). However, frankly speaking, that precedence in this case looks counter-intuitive to me as well.This case reminded me of John Resig's self-calling constructor pattern (given in Resig's blog entry Simple “Class” Instantiation in 2007). Nobody uses
new $
with Resig's jQuery and, I think, JSZip might follow jQuery's example and benefit from it.This pull request makes the
new
operator optional before JSZip's constructor. Programmers might continue usingnew
or omit it, depending on their inner personal preferences:new
+ space) might feel more RAD and less boilerplate code;new
might serve as a reminder that a constructor call is ahead;someZIP = require('jszip')()
instead ofsomeZIP = new (require('jszip'))()
means less brackets;(Unlike the example in Resig's blog, this pull request refrains from using
arguments.callee
because the use of that property is believed to slow down the engine.)This change introduces a new backwards-compatible feature. An
npm publish
of a new minor version is recommended.