Creating instantatable objects using pure JSON?? -- no prototyping!!

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
 
P

Peter Michaux

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

You might have a really great idea about what you are trying to
accomplish but I don't see how JSON fits into this picture even a
little tiny bit. JSON is for transmitting data between two systems.
That's it. JSON has to satisfy a particular regular expression and I
don't think/am pretty darn sure you can have functions in JSON. That
would go against the idea that JSON is a portable data exchange format
and a Perl system wouldn't know what to do with a JavaScript function,
for example.

<URL: http://json.org>
<URL: http://tech.groups.yahoo.com/group/ydn-javascript/>

Peter
 
C

ChrisO

[Original post snipped]

You might have a really great idea about what you are trying to
accomplish but I don't see how JSON fits into this picture even a
little tiny bit. JSON is for transmitting data between two systems.
That's it. JSON has to satisfy a particular regular expression and I
don't think/am pretty darn sure you can have functions in JSON. That
would go against the idea that JSON is a portable data exchange format
and a Perl system wouldn't know what to do with a JavaScript function,
for example.

Well, maybe I should use the term "JSON format" or something. I know
what you are saying, BUT... The notation itself is now being widely
used in JavaScript circles to create singleton objects and essentially
partitioned name spaces. You may or may not be familiar with this,
but I think the loosely coined term "JSON" applies to this as much as
to the pure use of the term "JSON" as a data-interchange format. I am
of course referring to the former usage.

ChrisO
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top