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
  • 267,341
  • 46
  • 444
  • 390
Ben_hawk
  • 2,476
  • 7
  • 34
  • 59

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
  • 74,352
  • 26
  • 153
  • 180
  • 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
  • 5,137
  • 1
  • 18
  • 29