JSON as datasource for prototype objects

H

holtmann

Hi,

I got a question regarding JSON as datasource- I understand that eval
on a JSON String creates the appropriate objects in JS.
But I would like to use JSON to supply data to already defined
prototpye objects. I try to give an example. I e.g. got:

address.prototype =
{
firstname: null;
lastname: null;
email: null;
//imagine a lot more properties here

getfirstname: function () { return this.firstname; },
checkadress: function() {//some code here;}
}

Imagine address has a lot of properties and methods. Now I want to use
JSON to create a new address-object and initialize the properties with
the data supplied in the JSON Object. How can I do this without having
to write a lot of code? What would the JSON Object have to look like?


Best,

Hendrik
 
A

aonic

There might be a better way, but the first thing I thought was a
function to pass additional data to the original definition.

Example:

var jsonObject = {
foo: 'foo',
bar: 'bar',

foo: function() {
alert(this.foo);
},

loadData: function(jsonData) {
for(index in jsonData) {
this[index] = jsonData[index];
}
},
};

//jsonObject.foo() => "foo"
//jsonObject.bar => "bar"

jsonObject.loadData( {
bar: function() {
alert(this.bar);
},

bar: 'foobar'
} );

//jsonObject.bar() => "foobar"
 
A

aonic

Sorry,

I misread, here is what you're probably looking for.

var jsonObject = function() {
this.bar = 'bar';

this.foo = function() {
alert(this.bar);
};
}
jsonObject.prototype = {
loadData: function(jsonData) {
for(index in jsonData) {
this[index] = jsonData[index];
}
}
};

var jObj = new jsonObject();

jObj.foo(); // => "bar"

jObj.loadData( {
bar: function() {
alert('foobar');
}
} );

jObj.bar(); // => "foobar"


There might be a better way, but the first thing I thought was a
function to pass additional data to the original definition.

Example:

var jsonObject = {
foo: 'foo',
bar: 'bar',

foo: function() {
alert(this.foo);
},

loadData: function(jsonData) {
for(index in jsonData) {
this[index] = jsonData[index];
}
},

};

//jsonObject.foo() => "foo"
//jsonObject.bar => "bar"

jsonObject.loadData( {
bar: function() {
alert(this.bar);
},

bar: 'foobar'

} );

//jsonObject.bar() => "foobar"

I got a question regarding JSON as datasource- I understand that eval
on a JSON String creates the appropriate objects in JS.
But I would like to use JSON to supply data to already defined
prototpye objects. I try to give an example. I e.g. got:
address.prototype =
{
firstname: null;
lastname: null;
email: null;
//imagine a lot more properties here
getfirstname: function () { return this.firstname; },
checkadress: function() {//some code here;}

Imagine address has a lot of properties and methods. Now I want to use
JSON to create a new address-object and initialize the properties with
the data supplied in the JSON Object. How can I do this without having
to write a lot of code? What would the JSON Object have to look like?

Hendrik
 

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

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,087
Latest member
JeremyMedl

Latest Threads

Top