View Full Version : javascript objects
brentnicholas
01-31-2008, 06:41 PM
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
Trinithis
01-31-2008, 11:57 PM
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.
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));
jscheuer1
02-01-2008, 06:58 AM
<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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.