Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to add method to JSON object?

Is there any way to add method to JSON object?

like image 533
uzay95 Avatar asked Jul 29 '10 07:07

uzay95


People also ask

Can JSON objects have methods?

JSON is a data format that has its own independent standard and libraries for most programming languages. JSON supports plain objects, arrays, strings, numbers, booleans, and null . JavaScript provides methods JSON.

Can we pass function in JSON?

Yes, you can.

How do I add data to an existing JSON object?

Use push() method to add JSON object to existing JSON array in JavaScript. Just do it with proper array of objects .

What is toJSON () in JSON?

The toJSON() method returns a date object as a string, formatted as a JSON date. JSON dates have the same format as the ISO-8601 standard: YYYY-MM-DDTHH:mm:ss.sssZ.


Video Answer


1 Answers

Yes. Javascript functions are first class objects, which means you can attach them to other objects as properties.

var myJson = { one: 1, two: 2 };

myJson.Sum = function() { return this.one + this.two; };

var result = myJson.Sum(); // result == 3
like image 131
Tomas Aschan Avatar answered Sep 21 '22 22:09

Tomas Aschan