Показать сообщение отдельно
  #3 (permalink)  
Старый Вчера, 23:18
Аватар для voraa
Профессор
Отправить личное сообщение для voraa Посмотреть профиль Найти все сообщения от voraa
 
Регистрация: 03.02.2020
Сообщений: 2,780

Сообщение от Aetae Посмотреть сообщение
без возможности присвоения, типа fillColor='#xxx', только c.fillColor='#xxx'):
Можно такие значения оформить как функции типа get/set
strokeStyle('#ff0000');
const ss = strokeStyle();


<body>
  <canvas id="canvas"></canvas>
  <script>
    const canvas = document.getElementById("canvas");
    const ctx = canvas.getContext("2d");

    function bindProxy(obj) {
      return new Proxy(obj, {
        get(target, key, r) {
          const targetproto = Object.getPrototypeOf(target);
          const value = Reflect.getOwnPropertyDescriptor(targetproto, key).value;
          //const value = Reflect.get(target, key, r);
          if (value && typeof value === 'function') {
            return value.bind(target);
          } else {
            const pget = Reflect.getOwnPropertyDescriptor(targetproto, key).get;
            const pset = Reflect.getOwnPropertyDescriptor(targetproto, key).set;
            if (pget && typeof pget === 'function') {
              if (pset && typeof pset === 'function') {
                return function (v) {
                  if (arguments.length === 0) return target[key];
                  target[key] = v;
                };
              } else {
                return function (v) {
                  return target[key];
                };
              }
            } else if (pset && typeof pset === 'function') {
              return function (v) {
                target[key] = v;
              };
            }
          }
          return value;
        }
      });
    }

    const { clearRect, strokeStyle, beginPath, moveTo, lineTo, stroke } = bindProxy(ctx);

    clearRect(0, 0, 1400, 150);
    strokeStyle('#ff0000');
    console.log(strokeStyle());
    beginPath();
    moveTo(0, 0);
    lineTo(100, 100);
    stroke()
  </script>
</body>
Ответить с цитированием