What is the difference between
RGBCatcher = new function(){}
and
var Basket = function(){}
One has new function()
whilst the other simply has function()
. Also one is using var
.
What is the difference between
RGBCatcher = new function(){}
and
var Basket = function(){}
One has new function()
whilst the other simply has function()
. Also one is using var
.
They're not jQuery objects. It's basic JavaScript syntax.
The difference between including a var
or not is that omitting a var
leaves the variable (RGBCatcher
) to be declared implicitly in the global scope, which is bad practise; you should always use a var
statement.
function
by itself declares a function (in this case it's a function expression), so you can call Basket()
to execute the function pointing to the Basket
variable.
new function
calls new
on the anonymous function created by the function
construct; It's the same as the following (except of course, you're not creating a function called Constructor
);
function Constructor() {
}
var RGBCatcher = new Constructor();
Please follow this thread:
`new function()` with lower case "f" in JavaScript
var a = new function(){
var member = '1';
alert(member);
}
// alerts 1
var b= function(){
alert('2');
return '2';
}();
// alerts 2
(function (){
alert ('3');
return '3';
})();
//alerts 3
alert (a);
// alerts [Object Object]
alert (b);
// alerts 2