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

    var p = new Point(this._z) // faills
    Archos, Feb 16, 2012
    1. Advertisements

  2. Archos

    Archos Guest

    I mean var p = new Point(Point._z)
    Archos, Feb 16, 2012
    1. Advertisements

  3. 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 *****

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


    <script type="text/javascript">

    function Point()
    switch (Point.arguments.length)
    case 1: // Copy other object
    case 2: // x and y provided
    default: // Bad signature.

    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
    alert("p4=("+p4.x+", "+p4.y+")");



    ***** End of Code Section *****


    Gene Wirchenko
    Gene Wirchenko, Feb 16, 2012
  4. Archos

    Archos Guest

    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
  5. [snip]
    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:
    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.
    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.)


    Gene Wirchenko
    Gene Wirchenko, Feb 16, 2012
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.