Get the name of an object

A

Andrew Poulos

If I have some javascript that look like this:

function Car() {
this.color = "red";
this.Describe = Car_Describe;
}

function Car_Describe() {
alert("My car is a " + this.color + " car");
}

var myCar = new Car();

myCar.Describe();


How can I pull the string "myCar" from *this* in the function Car_Describe?


Andrew Poulos
 
M

Michael Winter

[snip]
var myCar = new Car();

myCar.Describe();


How can I pull the string "myCar" from *this* in the function
Car_Describe?

You can't. You'd have to pass the identifier name as a string. Why do you
want to? I don't see any reason to.

Mike
 
R

Richard Cornford

Andrew said:
If I have some javascript that look like this:

function Car() {
this.color = "red";
this.Describe = Car_Describe;
}

function Car_Describe() {
alert("My car is a " + this.color + " car");
}

Objects created using functions as constructors are in a position to
take advantage of javascript's prototype based inheritance and acquire
their methods through that mechanism:-

function Car() {
this.color = "red";
}

Car.prototype.Describe = function() {
alert("My car is a " + this.color + " car");
}

Apart form making it unnecessary to assign the method to properties of
the object in the constructor (as each constructed object inherits the
method from its prototype instead), simplifying the code and speeding up
object construction, this approach also removes named functions that are
going to be used exclusively as object methods from the global
namespace. Doing so reduces the risk of naming collisions and unexpected
interactions.
var myCar = new Car();

myCar.Describe();

How can I pull the string "myCar" from *this* in the
function Car_Describe?

There is no relationship between an object instance and the name of an
identifier to which a reference to it may have been assigned. It should
be obvious that no such relationship can exist because if you do:-

var objectRef = myCar;

- how could - this - in the object know which of "objectRef" or "myCar"
you were interested in?

In practice object instances might need to be able to arrange some
indirect reference to themselves; calling methods of their own instance
form event handlers and the like. Taht can be arranged but different
approaches suit different contexts so more information would be need
before anything could be suggested.

Richard.
 
A

Andrew Poulos

Richard said:
Objects created using functions as constructors are in a position to
take advantage of javascript's prototype based inheritance and acquire
their methods through that mechanism:-

function Car() {
this.color = "red";
}

Car.prototype.Describe = function() {
alert("My car is a " + this.color + " car");
}

Apart from making it unnecessary to assign the method to properties of
the object in the constructor (as each constructed object inherits the
method from its prototype instead), simplifying the code and speeding up
object construction, this approach also removes named functions that are
going to be used exclusively as object methods from the global
namespace. Doing so reduces the risk of naming collisions and unexpected
interactions.




There is no relationship between an object instance and the name of an
identifier to which a reference to it may have been assigned. It should
be obvious that no such relationship can exist because if you do:-

var objectRef = myCar;

- how could - this - in the object know which of "objectRef" or "myCar"
you were interested in?

In practice object instances might need to be able to arrange some
indirect reference to themselves; calling methods of their own instance
from event handlers and the like. That can be arranged but different
approaches suit different contexts so more information would be need
before anything could be suggested.

Richard.

I've not really understood objects in javascript. What I want to do is
to slide a number of images, at the same time (more or less), into
predetermined positions. Something like:

function Car(x,y) {
this.x = x;
this.y = y
}

Car.prototype.MoveMe = function() {
// Is the car in position?
// No, then move it closer and call yourself again in 20ms
}

var myCar1 = new Car(10,20);
var myCar2 = new Car(20,30);
var myCar3 = new Car(30,40);

myCar1.MoveMe();
myCar2.MoveMe();
myCar3.MoveMe();

I get stuck trying to do iterations of MoveMe.

regards
Andrew Poulos
 

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
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top