2

What is the difference between these two methods?

function ObjectB() {
    this.methodA = new function() {
        alert('a');
    };
    this.methodB = function() {
        alert('b');
    };
}

What I am trying to ask, is what effect does the new have in a JS method?

I've done a fiddle where I wanted to explore the behaviour of the methods, and I have also added this code :

var v = Object.create(ObjectB);
v.methodC = function() {
    alert('c');
}

v.methodB();
v.methodA();
v.methodC();

But my fiddle doesnt seem to work.

Fiddle is here : http://jsfiddle.net/N8SNG/

Thanks :)

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • yes i saw that post. but that was showing how new was used when creating an object. Im talking about new function() – Oliver Watkins May 03 '13 at 11:53
  • Uh? `new` can **only** be used together with a function. `this.methodA = new function() { alert('a'); };` will basically create an object that is empty. It's not different than `this.methodA = new someFunction;`. The "call parenthesis" are optional in this case. `new` always does the same thing: Create an object and execute a constructor function. – Felix Kling May 03 '13 at 11:54
  • If you read that question's answers, you'll see that they all have `new function()` just like in this question. – JJJ May 03 '13 at 11:55
  • 2
    Have you been through this discussion ? http://stackoverflow.com/questions/2274695/new-function-with-lower-case-f-in-javascript – verisimilitude May 03 '13 at 12:01
  • yes, thanks, if anyone can get my fiddle working i would be grateful – Oliver Watkins May 03 '13 at 12:09
  • You didn't really understood what [`Object.create`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create) does? – Bergi May 03 '13 at 12:23

2 Answers2

3

methodA is not a method, because the new operator causes the function after it to be called as a constructor. So you get back an object for methodA with the anonymous function as the equivalent of its class.

It's as if you had written this:

var MethodA = function() {
    alert('a');
};
this.methodA = new MethodA;

And that last line is the same as this:

this.methodA = new MethodA();
Keen
  • 954
  • 12
  • 19
  • so, alert('a'), would pop up as the methodA is being read by the parser, and alert('b'), would pop up as the methodB is being called? – Oliver Watkins May 03 '13 at 12:06
  • 1
    The first alert is shown on instantiation of the object. The other, everytime the method is called. yes. If you compare to Java: the first alert is done in the constructor of the Object named "methodA". The second one is like a regular method. So everytime "methodA" is called, a new object is created and its constructor is invoked. – Thomas Junk May 03 '13 at 12:08
0

New is used on functions to create objects. These functions are constructors. As function creates functions, new function creates objects. When you use an anonymous function you create an object of kind "object". When you specify the name of the constructor function, you create an object of that kind: e.g.

function Human(){};
man=new Human();

man is of the kind "human", or better is an instance of Human:

man instanceof Human

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43