What does this (function(){}); , a function inside brackets, mean in javascript?
You're immediately calling an anonymus function with a specific parameter.
An example:
(function(name){
alert(name);
})('peter')
This alerts "peter".
In the case of jQuery you might pass jQuery
as a parameter and use $
in your function. So you can still use jQuery in noConflict-mode but use the handy $
:
jQuery.noConflict()
(function($){
var obj = $('<div/>', { id: 'someId' });
})(jQuery)
Why are parentheses used around a class name in Java?
What you're seeing is a cast to tell the compiler it's ok to assign a Cat to an Animal.
Casts are for cases where the compiler can't safely convert one type to another, either because it doesn't know what the types involved are, or because the conversion would lose information (for instance, if you have a double that you want to store in a variable of type float).
In your example the cast is totally unnecessary because Cat extends Animal; since all Cats are Animals, the compiler doesn't need an explicit cast to tell it it can assign a Cat to an Animal.
Casting should be reserved for special occasions. If you use explicit casts regularly then it may mean you're not getting the most benefit out of the type system.
What happens when you put a function in parentheses?
You mean something like the following?:
(function() {
// ...
})();
This basically ensures that any "var" declarations are kept private (they are scoped to the anonymous function in which they have been placed) rather than global. For example, consider:
var counter = 0;
window['inc'] = function() {
return counter++;
};
vs.
(function() {
var counter = 0;
window['inc'] = function() {
return counter++;
};
})();
Both of these have the effect of defining a function window.inc
that returns a counter that gets incremented; however, in the first version, counter
is actually visible to all other modules, because it is in the global scope, whereas the latter ensures that only window.inc
can access counter
.
Note that the code creates a function and immediately invokes it (that's what the last parens are for).
In javascript what does ( ... ) do exactly
The first example works for exactly the same reason the second example works. There's no real difference. This also works:
!function() { ... } ();
So does this:
var _dont_care_ = function() { ... } ();
The key is to arrange for the function
keyword to appear in the statement at a point where the parser knows that it cannot be the start of a function declaration statement. In such cases, the function
keyword must be instantiating a function object as a value in an expression.
Statements in JavaScript other than expression statements start with keywords (leave labels aside for the moment). Inside any statement that doesn't start with the function
keyword (going out on a limb here), the only thing that function
can mean is the instantiation of a function object value.
Extra parentheses on function
It's probably easier to understand if you leave the redundant parens out because they serve no purpose:
var something = function() {
return 3;
} // <-- a function.
(); // now invoke it and the result is 3 (because the return value is 3) assigned to variable called something
console.log(something) //3 because the function returned 3
var something = function() {
return 3;
}; // a function is assigned to a variable called something
console.log(something) //logs the function body because it was assigned to a function
console.log(something()) //invoke the function assigned to something, resulting in 3 being logged to the console because that's what the function returns
Python class definition syntax
The latter is a syntax error on older versions of Python. In Python 2.x you should derive from object
whenever possible though, since several useful features are only available with new-style classes (deriving from object
is optional in Python 3.x, since new-style classes are the default there).
What do these double parentheses do in JS?
This
(function(){
alert('hello');
})();
although it is a function is it called automatically so you dont/can't call it manually
These can be useful for for
loops like so
This will fail because i would be equal to 9 after 5 seconds
for(var i = 0; i < 10; i++) {
window.setTimeout(function(){
console.log(i);
}, 5000)
}
So you could do this
for(var i = 0; i < 10; i++) {
(function(a){
window.setTimeout(function(){
console.log(a);
}, 5000)
})(i);
}
Also good for creating a "private" scope like this
(function(){
var test = 'hello';
console.log( test ); // 'hello'
}());
console.log( test ); // 'undefined'
Related Topics
What Does "This" Refer to in Arrow Functions in Es6
Settimeout in For-Loop Does Not Print Consecutive Values
Detect When a Browser Receives a File Download
What Is the Reason to Use the 'New' Keyword At Derived.Prototype = New Base
Convert a JavaScript String in Dot Notation into an Object Reference
Jquery: Return Data After Ajax Call Success
Get Image Data Url in JavaScript
Loading Cross-Domain Endpoint With Ajax
Find the Min/Max Element of an Array in JavaScript
Correct Modification of State Arrays in React.Js
How to Create an Accurate Timer in JavaScript
Firebase Kicks Out Current User
How to Stop the Browser Back Button Using JavaScript
How to Convert Decimal to Hexadecimal in JavaScript
How Does JavaScript Handle Ajax Responses in the Background
Check If a User Has Scrolled to the Bottom (Not Just the Window, But Any Element)