Add a prototype to $dataItems [SOLVED]

Hi
Is possible to change the prototype of $dataItems
because i need to call fast function to build some regexr when is nessesery.

Or I would be better to create a new $variable in window, that when i will calling, will indirectly modify the $dataItems[]?
ex:
Code:
function itemsManage(){};
$items = new itemsManage();

itemsManage.prototype.doSomething = function(AddTXT) {

}
I looked at how the $dataItems semm to work, and I can not find the constructor and how is added to eatch array at gameBoot.
Do you have any idea about the line i need to check?
thank
$dataItems is an array, not a class. There is no prototype, and no constructor.
$dataItems is an array, not a class. There is no prototype, and no constructor.
I understand, I express myself badly, I speak in fact of what is found in the array.
aefaefae.jpg
the proto are OBJECT,
I wanted to know if it was possible to easily modify it to add additional functions.

But I think rather opt for the second solution.
I'm not like too mutch, to create a new variable just to add some (1 or 2) complementary function.

Code:
function itemsManage(){};
$items = new itemsManage();

itemsManage.prototype.doSomething = function(ItemID) {

}
_alias_DataItem_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
    _alias_DataItem_createGameObjects.apply(this, arguments);
    $items        = new itemsManage();
};
I wonder if you can add your function to the Object class
you can use .map to quickly replace $dataItems with a new list using your prototype.
I wonder if you can add your function to the Object class
According to my reading on the bases in javascript, it is better to avoid modifying the default class and library of javascript.

In fact I'm just looking to find the line that build this object in the array.
But without success.
I may do a bad search. !

idealy is for example, call $dataItems like this.
$dataItems[15].Dosomething('');
Otherwise, other way, I think it's not bad, but I'll have to call it by create a new variable.
$items(id).dosomething('');
you can use .map to quickly replace $dataItems with a new list using your prototype.
i dont know .map, i will look about it thanks.
i dont know .map, i will look about it thanks.
Basically you would do something like this:

Code:
$dataItems = $dataItems.map(function(item){
  if (item) {
    return new ItemClass(item);
  } else {
    return item;
  }
});

Then make your ItemClass with such a constructor:

Code:
function ItemClass(itemData) {
  for (var key in itemData) {
    if (itemData.hasOwnProperty(key) {
      this[key] = itemData[key];
    }
  }
}

I didn't test it here, but basically it would replace all items on $dataItems with an instance of your ItemClass. And the constructor of that class is just copying the data from the old item to the instance.
The line that builds the object into the array doesn't exist, because the array is created by the MV editor.
This should do:

Code:
var oldDataManager_onLoad = DataManager.onLoad;
DataManager.onLoad = function(object) {
  oldDataManager_onLoad.apply(this, arguments);

  if (object == $dataItems) {
    $dataItems = $dataItems.map(function(item){
      if (item) {
        return new ItemClass(item);
      } else {
        return item;
      }
    });
  }
};
This should do:

Code:
var oldDataManager_onLoad = DataManager.onLoad;
DataManager.onLoad = function(object) {
  oldDataManager_onLoad.apply(this, arguments);

  if (object == $dataItems) {
    $dataItems = $dataItems.map(function(item){
      if (item) {
        return new ItemClass(item);
      } else {
        return item;
      }
    });
  }
};

After doing a bit of reading on the .map, the idea is not bad but I think this will cause some performance problem.
This however gave me another idea.
I understood why the all $data not have constructor, simply because it is created by the the .json from the sofware rmmv database!

I made a small code, which changes the prototype of $dataItems
So when the game boot , or load save game , is change one time the prototype.
According to my test this work fine, maybe there are still many other ways to do it.
aecfava.jpg

Code:
function buildNewDataItemClass(){
    // make temp copy of $dataItems
    for(var i=0,itemTemp=[],max=$dataItems.length;i<max;i++){
        itemTemp[i] = Object.assign({}, $dataItems[i]);
    }
    for(var i=0,max=itemTemp.length; i<max; i++){
       $dataItems[i] = new itemsData(); // assign new class itemsData to $dataItems[i]
            for (var data in itemTemp[i]) {
                $dataItems[i][data] = itemTemp[i][data]; // replace data
            }
    }
}
function itemsData(){}; // new class for $dataItems

_alias_DataItem_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
    _alias_DataItem_createGameObjects.apply(this, arguments);
    buildNewDataItemClass();
  
};

I hope not to encounter a problem this way
It seems correct. !?
@Hudell I had not seen your last proposal, I will try it.
This should do:

Code:
var oldDataManager_onLoad = DataManager.onLoad;
DataManager.onLoad = function(object) {
  oldDataManager_onLoad.apply(this, arguments);

  if (object == $dataItems) {
    $dataItems = $dataItems.map(function(item){
      if (item) {
        return new ItemClass(item);
      } else {
        return item;
      }
    });
  }
};
Thank you infinitely for your solution hudell.
It is very ingenious, I just replace it, and it works wonderfully.
I think I'm going to learn more .map, because I did not get it in this that way.

[SOLVED]

Original thread: https://forums.rpgmakerweb.com/threads/add-a-prototype-to-dataitems-solved.79807/