473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Using 'self' in call to constructor

Bar = function (obj) {
// blah
}

var foo = new Bar(self);

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

Andrew Poulos
Jan 5 '06 #1
5 1487
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hayden Kirk | last post by:
Hey Is it possible to do without $this? I have got an instance of another class inside a class. Now I want to access this classes functions using the self keyword, but it seems i cant. Or...
5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
6
by: tkpmep | last post by:
I have a class with a name attribute, which I want to modify using property.The following code works just fine: class Portfolio(object): def __init__( self, name="Port1"): self.name=name ...
10
by: scrimp | last post by:
Ive been using PAMIE 1.4 to try to automate web page processes. The one thing I cannot do with it is upload files and download files. With uploading files, the file input box does not allow PAMIE...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
2
by: Tugrul HELVACI | last post by:
I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String; fPersonSurName : String; fPersonAge : Integer; published property PersonName : String...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
10
by: Evan Charlton | last post by:
Hey all, I'm having some trouble with window.setInterval() within a custom object/prototype. Here is my code: function MyClass() { // do some junk // ... // define methods this.m_one =...
1
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi, I was trying to find a way to set, upon __init__() the parent of a class to an existing instance. Here is a minimal example of what I'm trying to do: class A(object): def __init__(self, x):...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.