J
J
juwain2010-11-16 15:21:54
JavaScript
juwain, 2010-11-16 15:21:54

Logical negation of the result of executing an anonymous function

A sample of the following code was found in the facebook sorts. Please tell me the purpose and meaning of using such a technique: Original: Thank you!

!function(){

}();




!function(){
var c = document.documentElement;
var b = 'child_focused';
var d = 'DOMControl_placeholder';
var a = function(e){
e = e || window.event;
var f = e.target || e.srcElement, h = f.getAttribute('placeholder');
if (h) {
var g = Parent.byClass(f, 'focus_target');
if ('focus' == e.type || 'focusin' == e.type) {
if (f.value == h) {
f.value = '';
CSS.removeClass(f, d);
g && CSS.addClass(g, b);
}
}
else
if (f.value == '') {
CSS.addClass(f, d);
f.value = h;
g && CSS.removeClass(g, b);
}
}
};
c.onfocusin = c.onfocusout = a;
if (c.addEventListener) {
c.addEventListener('focus', a, true);
c.addEventListener('blur', a, true);
}
}();


Answer the question

In order to leave comments, you need to log in

3 answer(s)
[
[email protected]><e, 2010-11-16
@juwain

There's a meaning. If you do not write !, then the function will be recognized as a function declaration, and it cannot be called immediately, i.e.


function (){
/* body */
}()

Will raise an error. But by adding the operator, we "converted" our function declaration into a function expression, which can already be called according to the above scheme. Standard equivalent

(function (){
/* body */
})()
longer by 1 character.

A
Alexey Zhurbitsky, 2010-11-16
@blo

!function(){}() without return returns true anyway.
And the meaning depends on the context in which this function is performed.

J
juwain, 2010-11-16
@juwain

In this case, returning true as a result does not matter. The function is simply executed and the result is not assigned to anything and is not returned anywhere. What is the point then in using such a construct?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question