0

我正在阅读这篇关于制作自己的 VirtualDOM 的 Medium 文章并理解它,直到我遇到这个 JSfiddle,特别是第 14 行,它说$el.appendChild.bind($el);。看起来他正在尝试appendChild不带任何参数(仅使用 $el 作为this值)!我很混乱。有人可以解释吗?

这是代码:

/** @jsx h */

function h(type, props, ...children) {
  return { type, props, children };
}

function createElement(node) {
  if (typeof node === 'string') {
    return document.createTextNode(node);
  }
  const $el = document.createElement(node.type);
  node.children
    .map(createElement)
    .forEach($el.appendChild.bind($el));
  return $el;
}

const a = (
  <ul class="list">
    <li>item 1</li>
    <li>item 2</li>
  </ul>
);

const $root = document.getElementById('root');
$root.appendChild(createElement(a));
4

0 回答 0