Create object using values of a field

Discussion in 'Javascript' started by Archos, Feb 16, 2012.

  1. Archos

    Archos Guest

    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
     
    Archos, Feb 16, 2012
    #1
    1. Advertisements

  2. Archos

    Archos Guest

    On Feb 16, 5:21 pm, Archos <> wrote:
    > 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)
     
    Archos, Feb 16, 2012
    #2
    1. Advertisements

  3. On Thu, 16 Feb 2012 09:24:50 -0800 (PST), Archos <>
    wrote:

    >On Feb 16, 5:21 pm, Archos <> wrote:
    >> 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)


    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
     
    Gene Wirchenko, Feb 16, 2012
    #3
  4. Archos

    Archos Guest

    On Feb 16, 7:22 pm, Gene Wirchenko <> wrote:
    > On Thu, 16 Feb 2012 09:24:50 -0800 (PST), Archos <>
    > wrote:
    >
    > >On Feb 16, 5:21 pm, Archos <> wrote:
    > >> 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)

    >
    >      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
     
    Archos, Feb 16, 2012
    #4
  5. On Thu, 16 Feb 2012 13:53:00 -0800 (PST), Archos <>
    wrote:

    [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
     
    Gene Wirchenko, Feb 16, 2012
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.