Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

javascript - ES6 to ES3 translation for MediaWiki (foreach() method and spreading operator)

I desire to execute the following tested, working in console, ES6 code through MediaWiki as described in detail here,

but it fails being executed through MediaWiki as its latest release for now (1.33.0) allegedly can only execute ES3:

const list = document.querySelector("#footnotes_list");
document.querySelectorAll(".footnote>sup").forEach((footnote, i) => {
    const li = document.createElement("li");
    li.append(...footnote.childNodes); // move content
    list.appendChild(li);
    footnote.textContent = i+1;
});

I assume const is okay to become var;
I read in W3schools that forEach() is ES5 but AFAICR I can replace this with for loop.

I miss what could replace array spreading via spread operator (...) in ES3.
Google search for "array spreading ES3" (without quote marks) brings no example on how to spread an array in ES3 (maybe the terminology is different?).

If possible within the limitations of ES3, how will you do array spreading with it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code, "babelified" here:

"use strict";

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

var list = document.querySelector("#footnotes_list");
document.querySelectorAll(".footnote>sup").forEach(function (footnote, i) {
  var li = document.createElement("li");
  li.append.apply(li, _toConsumableArray(footnote.childNodes)); // move content

  list.appendChild(li);
  footnote.textContent = i + 1;
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...