0

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.

Michael Berkowski
  • 253,311
  • 39
  • 421
  • 371
Ben_hawk
  • 2,430
  • 7
  • 29
  • 58

2 Answers2

2

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(); 
Matt
  • 70,063
  • 26
  • 142
  • 172
  • I dont understand why you need a variable named Basket for a function. Whats the difference between saying function Baset() and var Basket = function(){} – Ben_hawk Jul 12 '12 at 13:15
  • @Ben_hawk: See http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip – Matt Jul 12 '12 at 13:16
0

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

Community
  • 1
  • 1
srini.venigalla
  • 4,945
  • 1
  • 15
  • 29