Using 'self' in call to constructor

A

Andrew Poulos

Bar = function (obj) {
// blah
}

var foo = new Bar(self);

In Bar is 'this' and 'obj' the same thing?

Andrew Poulos
 
R

Randy Webb

Andrew Poulos said the following on 1/5/2006 4:01 AM:
Bar = function (obj) {
// blah
}

var foo = new Bar(self);

In Bar is 'this' and 'obj' the same thing?

Bar = function (obj) {
alert(obj === this)
// blah
}

var foo = new Bar(self);

No, as the alert shows false.
 
V

VK

Andrew said:
Bar = function (obj) {
// blah
}

var foo = new Bar(self);

In Bar is 'this' and 'obj' the same thing?

Taking out occasional situations where global context doesn't have
"window" host object:

"self" is reference to the current window (=== window.self)

This way
var foo = new Bar(self);
sends reference to the current window to constructor. Naturally it
cannot be equal to "this" in the constructor context - unless you
specially apply yourselve to break the constructor.

So the posed question doesn't have too much of sense. A wild guess
would be that you're playing with inheritance, but not sure how to do
it. More details would help greatly.
 
T

Thomas 'PointedEars' Lahn

Andrew said:
Bar = function (obj) {

If this is not in a BlockStatement, it should be

function Bar(obj)
{

so that a declaration takes place, not an probably undeclared definition.
// blah
}

var foo = new Bar(self);

In Bar is 'this' and 'obj' the same thing?

Of course not. The above example works only if there is an object in the
scope chain that has the `self' property (otherwise it results in a
ReferenceError). In an HTML UA environment this object is a Window object.
It's `self' property refers to that Window object itself (so that property
is in fact quite useless).

In `Bar', when it is used as a constructor due to the `new' keyword, `this'
refers to the object created(/constructed), which is a Bar object, that is,
an object that has the Object object and Bar.prototype in its prototype
chain; not a Window object, and certainly not the Window object that may be
in the scope chain.

But in `Bar', when it is instead called [on absence of the `new' keyword but
presence of the Call Operator `('...`)'], `this' could refer to the same
object as `self' does, if `Bar' is defined in global context or used in
local context but was not declared before.


PointedEars
 
T

Thomas 'PointedEars' Lahn

VK said:
Andrew said:
Bar = function (obj) {
// blah
}

var foo = new Bar(self);

In Bar is 'this' and 'obj' the same thing?

[...]
This way
var foo = new Bar(self);
sends reference to the current window to constructor. Naturally it
cannot be equal to "this" in the constructor context - unless you
specially apply yourselve to break the constructor.

Since `this' cannot be assigned a value, equality could
only be accomplished by assigning `this' to `obj'.


PointedEars
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top