new NewExpression

  • Thread starter Dmitry A. Soshnikov
  • Start date
D

Dmitry A. Soshnikov

One interesting question; it fits for quiz question. Answer without
checks, it's easy:

function A() {
this.getB = function () {
return function B() {
};
};
}

var x = new new A().getB();
alert(x); ?

Dmitry.
 
D

Doug Gunnoe

One interesting question; it fits for quiz question. Answer without
checks, it's easy:

function A() {
  this.getB = function () {
    return function B() {
    };
  };

}

var x = new new A().getB();
alert(x); ?

Dmitry.

Ok.

So why is this different?

function A() {
this.getB = function () {
return function B() {
};
};


}


var x = new (new A().getB());
alert(x);

In IE 7 I get [Object object]

and if I do this,

function A() {
this.getB = function () {
return function B() {
alert('what?');
};
};


}

var x = new (new A().getB());
alert(x);

I get 'what?' then [Object object]

I assume the first has something to do with precedent, but why would
function B be called in the second example?
 
T

Thomas Allen

One interesting question; it fits for quiz question. Answer without
checks, it's easy:
function A() {
  this.getB = function () {
    return function B() {
    };
  };

var x = new new A().getB();
alert(x); ?

Ok.

So why is this different?

function A() {
  this.getB = function () {
    return function B() {
     };
  };

}

var x = new (new A().getB());
alert(x);

In IE 7 I get [Object object]

and if I do this,

function A() {
  this.getB = function () {
    return function B() {
        alert('what?');
    };
  };

}

var x = new (new A().getB());
alert(x);

I get 'what?' then [Object object]

I assume the first has something to do with precedent, but why would
function B be called in the second example?

In both cases, x is an instance of B. In the second example
alert('what?') is run in the constructor before the instance is
returned, because that's how constructors work.

Thomas
 
L

Lasse Reichstein Nielsen

Doug Gunnoe said:
On Mar 19, 8:26 am, "Dmitry A. Soshnikov" <[email protected]>
wrote:
So why is this different? ....
var x = new (new A().getB());
and if I do this,

function A() {
this.getB = function () {
return function B() {
alert('what?');
};
};
}

var x = new (new A().getB());
alert(x);

I get 'what?' then [Object object]

I assume the first has something to do with precedent, but why would
function B be called in the second example?

It's called in both examples - as a constructor.

The implicit associtaion in Dmitry's example is:
new (new A().getB)()
because the new operator binds stronger than the call operator.

The calls A as a constructor, creating an object with a getB property,
reads that getB property and uses that function as a constructor.
That function returns the function B instead of the created object,
and alerts B's string represetntation.

The, explicit, association in your example is:
new (new A().getB())
which is equivalent to
new (new A().getB())()

It calls A as a constructor, creating an object with a getB property,
calls that property as a method, which returns the function B, and
then calls B as a constructor, returning a new Object (and alerting
"what?"). Then it alerts that object, giving "[object Object]".

/L
 
D

Doug Gunnoe

In both cases, x is an instance of B. In the second example
alert('what?') is run in the constructor before the instance is
returned, because that's how constructors work.

Thomas- Hide quoted text -

- Show quoted text -

But the 'constructor' for B is not being called when it is like this:

var x = new new A().getB();

How can x be an instance of B if the 'constructor' is not called?
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Doug said:
Dmitry A. Soshnikov said:
One interesting question; it fits for quiz question. Answer without
checks, it's easy:

function A() {
this.getB = function () {
return function B() {
};
};

}
var x = new new A().getB();
alert(x); ?

Dmitry.

Ok.

So why is this different?

function A() {
this.getB = function () {
return function B() {
};
};

}

var x = new (new A().getB());
alert(x);

In IE 7 I get [Object object]

and if I do this,

function A() {
this.getB = function () {
return function B() {
alert('what?');
};
};

}

var x = new (new A().getB());
alert(x);

I get 'what?' then [Object object]

I assume the first has something to do with precedent, but why would
function B be called in the second example?

In both cases, x is an instance of B. In the second example
alert('what?') is run in the constructor before the instance is
returned, because that's how constructors work.

It is misleading if not wrong terminology to say that "X is an instance of
Y" if there is no inheritance relation between X and Y. Do not let
yourself be confused by the `instanceof' operator and the fact that Y is
the identifier of reference to a constructor: these are prototype-based
programming languages, and Y, the constructor, is _not_ in the prototype
chain of X here (the value of Y.prototype when X was constructed is).

The ECMAScript Language Specification, Editions up to 5, employs the term
"instance" only to distinct this prototype-based language from class-based
ones, and in "Y instance", e.g. "Object instance."

It would therefore be correct to say "x stores a reference to an object
that is a B instance" were it not for the fact that there is no `B' outside
of the body of the referred constructor function, implementation bugs
aside.


PointedEars
 
D

Dmitry A. Soshnikov

How can x be an instance of B if the 'constructor' is not called?

Yes, it was a question on precedent and as it was correctly mentioned
above, `new` has higher precedent than call brackets.

So,

var x = new new A().getB(); // function B

transforms to:

new (new A().getB)();

or simply to

new (new A().getB); as there are no arguments

and to get instance of constructor B we need to use:

var x = new new new A().getB(); // [object Object]

or explicit grouping operator:

var x = new (new A().getB()); // [object Object]

Dmitry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top