Results 1 to 3 of 3

Thread: javascript objects

  1. #1
    Join Date
    Jan 2007
    Posts
    51
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default javascript objects

    This is probably simple for those who do alot with Prototype and js 'objects'.

    I've got an object called 'Validate.Error'...

    when I do an alert on it I get:
    function(A){this.message=A;this.name="ValidationError"}

    So how do I get just the value of this.name out of it?

    I've tried:
    Validation.name
    Error.name
    Error(name)

    Thoughts?

    Thanks,

    BN

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    I would have to see a bit more of your code. As a side note, you don't want to use uppercased variables that aren't constructors (upper camel case) or constants (all capped). In any case here's a little demo on how to use OOP in JS.

    Code:
    function Square(w, h) {
      this.width = w;
      this.height = h;
    }
    
    Square.prototype.calcArea = function() {
      return this.width * this.height;
    };
    
    Square.prototype.calcPerimiter = function() {
      return 2 * (this.width + this.height);
    };
    
    Square.prototype.compareTo = function(square) {
      var a = this.calcArea();
      var b = square.calcArea();
      if(a < b)
        return -1;
      if(a > b)
        return 1;
      return 0;
    };
    
    var sq1 = new Square(2, 5);
    var sq2 = new Square(1, 1);
    
    alert(sq1.compareTo(sq2));
    Last edited by Trinithis; 02-01-2008 at 12:02 AM. Reason: spelling
    Trinithis

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript">
    var Validate = {
    Error:function(A){this.message=A;this.name="ValidationError"}
    }
    Validate.Error('bob');
    alert(Validate.name)
    </script>
    In the above arrangement, which I think represents what you laid out - in an object Validate with a property named Error which is a function and therefore also an object in its own right when expressed as Validate.Error, Validate.name has no value until Validate.Error is run.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •