Create object using values of a field

A

Archos

function Point(x, y) { this._z=0,0; this.x=x; this.y=y; }

How to create a new object with the values that are in field
'this._z'?

var p = new Point(this._z) // faills
 
A

Archos

function Point(x, y) { this._z=0,0; this.x=x; this.y=y; }

How to create a new object with the values that are in field
'this._z'?

var p = new Point(this._z) // faills

I mean var p = new Point(Point._z)
 
G

Gene Wirchenko

I mean var p = new Point(Point._z)

You did not write very clearly so I am restating your problem.

I think that you want to be able to create Point object either by
specifying x and y or by specifying an existing Point object whose
values are to be used.
var p1=new Point(3,4);
var p2=p1;

If your question is how to have two different object constructors
and one is not just an object copy, use .arguments. See the code
below. I did not put error handling for the case of when the
signature is wrong.

***** Start of Code Section *****
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>2012-02-16: Multiple Signature Constructor</title>
</head>

<body>

<script type="text/javascript">

function Point()
{
switch (Point.arguments.length)
{
case 1: // Copy other object
this.x=Point.arguments[0].x;
this.y=Point.arguments[0].y;
break;
case 2: // x and y provided
this.x=Point.arguments[0];
this.y=Point.arguments[1];
break;
default: // Bad signature.
alert("ERROR");
}
}

p1=new Point(3,4);
alert("p1=("+p1.x+", "+p1.y+")");
p2=new Point(p1);
alert("p2=("+p2.x+", "+p2.y+")");
p3=new Point(1,2,3); // bad
p4=p1;
alert("p4=("+p4.x+", "+p4.y+")");

</script>

</body>

</html>
***** End of Code Section *****

Sincerely,

Gene Wirchenko
 
A

Archos

     You did not write very clearly so I am restating your problem.

     I think that you want to be able to create Point object eitherby
specifying x and y or by specifying an existing Point object whose
values are to be used.
          var p1=new Point(3,4);
          var p2=p1;

     If your question is how to have two different object constructors
and one is not just an object copy, use .arguments.  See the code
below.  I did not put error handling for the case of when the
signature is wrong.
Sorry for my english.

I want to that several objects (Point() is one of them) have a
'prototype' to return the values that have been set in field '_z'
Point.prototype.GetZero = function() {...}

But I want not to create that signature for each object
 
G

Gene Wirchenko

[snip]
I want to that several objects (Point() is one of them) have a
'prototype' to return the values that have been set in field '_z'
^^^^^
I am not sure what you mean here by this word.

By your usage, it appears it could be a Point object.

It might also be a string of values to assign to the Point
properties. In that case, it would be better to have _z be an array
with each element containing a property value.

Or it could be something else. Try reading this:
http://www.catb.org/~esr/faqs/smart-questions.html
It may help your organise your thoughts better so you either figure it
out yourself or can explain yourself better in your requests for help.
Point.prototype.GetZero = function() {...}

But I want not to create that signature for each object

You do not. You create it for the object type (Point). Objects
of that type then have that method. (This may not be totally correct,
but if you set up your types and do not modify them after declaring
objects of those types, it is correct.)

Sincerely,

Gene Wirchenko
 

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
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top