By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,045 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Using 'self' in call to constructor

P: n/a
Bar = function (obj) {
// blah
}

var foo = new Bar(self);

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

Andrew Poulos
Jan 5 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 5 '06 #2

P: n/a
VK

Andrew Poulos wrote:
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.

Jan 5 '06 #3

P: n/a
Andrew Poulos wrote:
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
Jan 5 '06 #4

P: n/a
VK wrote:
Andrew Poulos wrote:
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
Jan 5 '06 #5

P: n/a
Thomas 'PointedEars' Lahn wrote:
Since `this' cannot be assigned a value, equality could
only be accomplished by assigning `this' to `obj'.

You cannot assign 'this' a value, but the conclusion isn't entirely true:
you can get the reverse effect (of assigning 'obj' to 'this') if you really
want it:

Bar = function (obj) {
if (this!==obj) return Bar.call(obj,obj);
// blah
this.name = 'oops';
return this;
}

var foo = new Bar(self);
alert(["foo",foo.name,"self",self.name,foo===self].join(' '));

The alert will be:

foo oops self oops true

(I learned something new today. I hadn't realised before that Ecmascript
actually allows you to return a result from a constructor and the result
takes precedence over the newly created object, so I guess you can
implement flyweight and singleton patterns that way if you want).
Jan 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.