JSFiddle - React, Tailwind, and code Playground
by pz_godlin
JavaScript
// ПРИМЕР ПОСТРОЕНИЯ СЕРИАЛИЗУЕМЫХ ОБЪЕКТОВ С ВОЗМОЖНОСТЬЮ НАСЛЕДОВАНИЯ
function w (s)
{
document.write(s);
}
Serializable_obj_types = ({ });
Serializable = function (cfg)
{
this.obj_type = ( typeof(cfg.obj_type) != 'undefined' ) ? cfg.obj_type : 'Serializable';
this.a = 1;
this.b = 2;
this.test1 = 'УРА!';
this.register_obj_type = function (obj_type)
{
Serializable_obj_types [obj_type] = 1;
if ( typeof(cfg.obj_type) != 'undefined' )
Serializable_obj_types [cfg.obj_type] = 1;
}
this.register_obj_type(this.obj_type);
// десериализует строку в текущий объект
this.load_json = function ( json_data )
{
var obj_data = JSON.parse(json_data);
this.load_obj_data(obj_data);
}
this.create_object = function (obj_data)
{
if ( typeof(obj_data.obj_type) != 'undefined' )
{
if( Serializable_obj_types[obj_data.obj_type] == 1 )
//obj_data = eval( 'new '+ obj_data.obj_type +'({ obj_data : obj_data })');
obj_data = new ( eval(obj_data.obj_type) ) ({ obj_data : obj_data });
}
return obj_data;
}
// десериализует безымянный объект в текущий объект
this.load_obj_data = function ( obj_data )
{
// перекидываем данные из безымянного объекта в текущий
for (var i in obj_data) this[i] = obj_data[i];
// если есть потомки, обработаем их тоже:
if ( typeof(this['children']) != 'undefined' )
{
for (var ch=0; ch< this['children'].length; ++ch)
{
this['children'][ch] = this.create_object(this['children'][ch]);
}
}
}
this.get_json = function ()
{
...