P
Passiday
Hello,
Reading the JQuery API, I've paused on this page:
http://api.jquery.com/category/events/event-object/
It says there "The new operator is optional.", and shows two examples:
//Create a new jQuery.Event object without the "new" operator.
var e = jQuery.Event("click");
// Create a new jQuery.Event object with the "new" operator
(optional).
var e = new jQuery.Event("click");
Now, how this is achieved? If, in the first line of constructor
function, I can detect that it is called without "new" operator, I can
call it internally and then return the result. So, apparently the
trick is in this detection. Is it done by testing (typeof this ==
"undefined")? I am wondering if there are contexts where this can be
fooled: one is closure and other is when you call the construction
function's call or apply method. The first problem is fully controlled
by the developer of class, and the other problem can be controlled by
kindly asking users not to do like that.
Is this how it is done?
Reading the JQuery API, I've paused on this page:
http://api.jquery.com/category/events/event-object/
It says there "The new operator is optional.", and shows two examples:
//Create a new jQuery.Event object without the "new" operator.
var e = jQuery.Event("click");
// Create a new jQuery.Event object with the "new" operator
(optional).
var e = new jQuery.Event("click");
Now, how this is achieved? If, in the first line of constructor
function, I can detect that it is called without "new" operator, I can
call it internally and then return the result. So, apparently the
trick is in this detection. Is it done by testing (typeof this ==
"undefined")? I am wondering if there are contexts where this can be
fooled: one is closure and other is when you call the construction
function's call or apply method. The first problem is fully controlled
by the developer of class, and the other problem can be controlled by
kindly asking users not to do like that.
Is this how it is done?