D
D
Dmitry Klimantovich2022-01-13 18:47:39
1C-Bitrix
Dmitry Klimantovich, 2022-01-13 18:47:39

How to pass PHP array to JS via CUtil::PHPToJSObject()?

Good afternoon.
I'm confused and don't understand. Please explain why:
In the file result_modifier.php at the end of the file, I write to a new array some data from $arResult (they are sorted out a little earlier and stored in $arElementsToJS)

?><script type="text/javascript">
  var arElementsObj = new arElements(<?=CUtil::PHPToJSObject($arElementsToJS); // передаем массив в JS?>);
</script><?

In the sript.js file, I accept them:
arElements = function (arParams) {
  if (typeof arParams === 'object')
  {
    this.arParams = arParams;
    console.log(arParams);
  }
};

Thus, in the console, I see Object and everything is fine.

But how can this be written to a variable outside of the function? Those. this way in the script.js file doesn't work.:
var newvar = '';
arElements = function (arParams) {
  if (typeof arParams === 'object')
  {
    newvar = arParams;
  }
};
console.log(newvar);

Please explain why?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question