C
ChrisO
I've been pretty infatuated with JSON for some time now since
"discovering" it a while back. (It's been there all along in
JavaScript, but it was just never "noticed" or used by most until
recently -- or maybe I should just speak for myself.)
The fact that JSON is more elegant goes without saying, yet I can't
seem to find a way to use JSON the way I *really* want to use it: to
create objects that can be instantated into multiple instances without
prototyping. I've seen (and used) JSON for singleton object instances
-- this not a problem and this is how it works right out of the gate.
But given the following custom object written the past "normal" way, I
would like to write it in JSON format, and then be able to create new
instances from the one definition. Here's an example using the "old
way" most who have been writing JavaScript for years have seen:
function Two( x, y ) {
this.x = x;
this.y = y;
}
Two.prototype.sum = function () { return this.x + this.y }
Two.prototype.max = function () { return this.x > this.y ? this.x :
this.y }
Two.prototype.min = function () { return this.x > this.y ? this.y :
this.x }
Two.prototype.pow = function () { return Math.pow( this.x, this.y ) }
Now, I know I can get all "fancy" with the above and do either this:
Two.prototype = {
sum : function { return this.x + this.y },
max : function () { return this.x > this.y ? this.x : this.y },
min : function () { return this.x > this.y ? this.y : this.x },
pow : function () { return Math.pow( this.x, this.y ) }
};
Or this:
function Two( x, y ) {
// Properties.
this.x = x;
this.y = y;
// Methods.
this.sum = function () { return this.x + this.y }
this.max = function () { return this.x > this.y ? this.x : this.y }
this.min = function () { return this.x > this.y ? this.y : this.x }
this.pow = function () { return Math.pow( this.x, this.y ) }
}
(The later seems to work without prototyping...!)
But neither are really as close to pure JSON as I would like, so that
I can instantate those:
var hisPair = new Two( 11, 22 );
var herPair = new Two( 33, 44 );
What I'd like is a way in PURE JSON to be able to create the Two class
(as an example) using pure JSON. I've not seen anything yet on the
web that addresses this directly aside from some pages which require
you to include another JS to allow "deep embedding" of classes using
other helper "classes" (that are created the "old way" it seems), etc.
The best I've found so far on using pure JSON to create a class that
allows *multiple* instances is something like this:
function Two( x, y ) {
var class = {
x : x,
y : y,
sum : function { return this.x + this.y }
max : function () { return this.x > this.y ? this.x : this.y }
min : function () { return this.x > this.y ? this.y : this.x }
pow : function () { return Math.pow( this.x, this.y ) }
};
for (var element in class) this[element] = class[element];
}
Now *THAT* works, but it's still not as "pure" I would like. But it's
acceptable for now, I guess, since I *am* creating the entire "class"
as a JSON object, and I consider the outside function "wrapper" as the
necessary "constructor." But I keep wondering... There HAS to be a
better way.
I'm just wondering if anyone knows of a place that discusses JSON used
in situations like the above. Again, I've seen an ABUNDANCE of pages
and sites that discuss JSON in Singleton usage, but nothing that
discusses it as I am wanting here.
Thanks!
ChrisO
"discovering" it a while back. (It's been there all along in
JavaScript, but it was just never "noticed" or used by most until
recently -- or maybe I should just speak for myself.)
The fact that JSON is more elegant goes without saying, yet I can't
seem to find a way to use JSON the way I *really* want to use it: to
create objects that can be instantated into multiple instances without
prototyping. I've seen (and used) JSON for singleton object instances
-- this not a problem and this is how it works right out of the gate.
But given the following custom object written the past "normal" way, I
would like to write it in JSON format, and then be able to create new
instances from the one definition. Here's an example using the "old
way" most who have been writing JavaScript for years have seen:
function Two( x, y ) {
this.x = x;
this.y = y;
}
Two.prototype.sum = function () { return this.x + this.y }
Two.prototype.max = function () { return this.x > this.y ? this.x :
this.y }
Two.prototype.min = function () { return this.x > this.y ? this.y :
this.x }
Two.prototype.pow = function () { return Math.pow( this.x, this.y ) }
Now, I know I can get all "fancy" with the above and do either this:
Two.prototype = {
sum : function { return this.x + this.y },
max : function () { return this.x > this.y ? this.x : this.y },
min : function () { return this.x > this.y ? this.y : this.x },
pow : function () { return Math.pow( this.x, this.y ) }
};
Or this:
function Two( x, y ) {
// Properties.
this.x = x;
this.y = y;
// Methods.
this.sum = function () { return this.x + this.y }
this.max = function () { return this.x > this.y ? this.x : this.y }
this.min = function () { return this.x > this.y ? this.y : this.x }
this.pow = function () { return Math.pow( this.x, this.y ) }
}
(The later seems to work without prototyping...!)
But neither are really as close to pure JSON as I would like, so that
I can instantate those:
var hisPair = new Two( 11, 22 );
var herPair = new Two( 33, 44 );
What I'd like is a way in PURE JSON to be able to create the Two class
(as an example) using pure JSON. I've not seen anything yet on the
web that addresses this directly aside from some pages which require
you to include another JS to allow "deep embedding" of classes using
other helper "classes" (that are created the "old way" it seems), etc.
The best I've found so far on using pure JSON to create a class that
allows *multiple* instances is something like this:
function Two( x, y ) {
var class = {
x : x,
y : y,
sum : function { return this.x + this.y }
max : function () { return this.x > this.y ? this.x : this.y }
min : function () { return this.x > this.y ? this.y : this.x }
pow : function () { return Math.pow( this.x, this.y ) }
};
for (var element in class) this[element] = class[element];
}
Now *THAT* works, but it's still not as "pure" I would like. But it's
acceptable for now, I guess, since I *am* creating the entire "class"
as a JSON object, and I consider the outside function "wrapper" as the
necessary "constructor." But I keep wondering... There HAS to be a
better way.
I'm just wondering if anyone knows of a place that discusses JSON used
in situations like the above. Again, I've seen an ABUNDANCE of pages
and sites that discuss JSON in Singleton usage, but nothing that
discusses it as I am wanting here.
Thanks!
ChrisO