I'm using a node server to send some objects to my front end code. I've added some methods to my objects using a prototype, but when I send them to the front end the prototype method disappears. The objects still have a property with the prototype method name, but the value has been set to undefined. Is there a way that I can recover the prototype method on the client side?
My code looks like the following:
server.js
function Class (data) {
this.value = data;
}
Class.prototype.isEven = function() {
return this.value % 2 === 0;
};
app.get('/data', function(req, res) {
db.query('select * from table', function(err, rows, fields) {
var items = [];
for (var i = 0; i < rows.length; i++) {
items.push(
new Class(rows[i]['value'])
);
}
res.send(items);
});
});
client.js
$.ajax({url: "/data", type: "GET", success: function(data) {
var item = data[0];
console.log(item.isEven); // displays 'undefined'
});