需要帮助来浓缩我的JavaScript代码,该代码在一个函数中

时间:2019-12-06 00:22:29

标签: javascript

基本上,我正在尝试为动物创建爪子,我想知道如何压缩我的代码。该代码创建了一行,向前移动了15个空格后便会移动。它已经在一个函数中。我只能使用此变量:

for (var i = 0; i < 4; i++) {

}

和此功能:

function myFunction() {

}
function drawHands () {
  penUp();
  moveTo(215, 280);
  penColor("black");
  penDown();
  penWidth(3);
  turnTo(0);
  moveForward(15);
  penUp();
  moveTo(230, 280);
  penDown();
  moveForward(15);
  penUp();
  moveTo(265, 280);
  penDown();
  moveForward(15);
  penUp();
  moveTo(280, 280);
  penDown();
  moveForward(15);
}
drawHands();

2 个答案:

答案 0 :(得分:1)

您具有一些类似的重复功能:在<?php namespace App\Library\Cache\Backend; use Phalcon\Cache\Exception; class Redis extends \Phalcon\Cache\Backend\Redis { /** * @var \Redis */ protected $_redis; /** * {@inheritdoc} * * @param string $keyName * @param integer $lifetime * @return mixed|null */ public function get($keyName, $lifetime = null) { $redis = $this->getRedis(); /** * @var \Phalcon\Cache\FrontendInterface $frontend */ $frontend = $this->_frontend; $lastKey = $this->getKeyName($keyName); $this->_lastKey = $lastKey; $content = $redis->get($lastKey); if ($content === false) { return null; } if (is_numeric($content)) { return $content; } return $frontend->afterRetrieve($content); } /** * {@inheritdoc} * * @param string $keyName * @param string $content * @param int $lifetime * @param bool $stopBuffer * @return bool * * @throws Exception */ public function save($keyName = null, $content = null, $lifetime = null, $stopBuffer = true) { if ($keyName === null) { $lastKey = $this->_lastKey; } else { $lastKey = $this->getKeyName($keyName); $this->_lastKey = $lastKey; } if (!$lastKey) { throw new Exception('The cache must be started first'); } $redis = $this->getRedis(); /** * @var \Phalcon\Cache\FrontendInterface $frontend */ $frontend = $this->_frontend; if ($content === null) { $cachedContent = $frontend->getContent(); } else { $cachedContent = $content; } /** * Prepare the content in the frontend */ if (!is_numeric($cachedContent)) { $preparedContent = $frontend->beforeStore($cachedContent); } else { $preparedContent = $cachedContent; } if ($lifetime === null) { $tmp = $this->_lastLifetime; $ttl = $tmp ? $tmp : $frontend->getLifetime(); } else { $ttl = $lifetime; } $success = $redis->set($lastKey, $preparedContent); if (!$success) { throw new Exception('Failed storing the data in redis'); } if ($ttl > 0) { $redis->setTimeout($lastKey, $ttl); } $isBuffering = $frontend->isBuffering(); if ($stopBuffer === true) { $frontend->stop(); } if ($isBuffering === true) { echo $cachedContent; } $this->_started = false; return $success; } /** * {@inheritdoc} * * @param string $keyName * @return bool */ public function delete($keyName) { $redis = $this->getRedis(); $lastKey = $this->getKeyName($keyName); return (bool)$redis->delete($lastKey); } /** * {@inheritdoc} * * @param string $prefix * @return array */ public function queryKeys($prefix = null) { $redis = $this->getRedis(); $pattern = "{$this->_prefix}" . ($prefix ? $prefix : '') . '*'; return $redis->keys($pattern); } /** * {@inheritdoc} * * @param string $keyName * @param string $lifetime * @return bool */ public function exists($keyName = null, $lifetime = null) { $redis = $this->getRedis(); if ($keyName === null) { $lastKey = $this->_lastKey; } else { $lastKey = $this->getKeyName($keyName); } return (bool)$redis->exists($lastKey); } /** * {@inheritdoc} * * @param string $keyName * @param int $value * @return int */ public function increment($keyName = null, $value = 1) { $redis = $this->getRedis(); if ($keyName === null) { $lastKey = $this->_lastKey; } else { $lastKey = $this->getKeyName($keyName); } return $redis->incrBy($lastKey, $value); } /** * {@inheritdoc} * * @param string $keyName * @param int $value * @return int */ public function decrement($keyName = null, $value = 1) { $redis = $this->getRedis(); if ($keyName === null) { $lastKey = $this->_lastKey; } else { $lastKey = $this->getKeyName($keyName); } return $redis->decrBy($lastKey, $value); } /** * {@inheritdoc} * * @return bool */ public function flush() { } /** * Get Prefix * * @return string */ public function getPrefix() { return $this->_prefix; } /** * Get Redis Connection * * @return \Redis */ public function getRedis() { $redis = $this->_redis; if (!is_object($redis)) { $this->_connect(); $redis = $this->_redis; } return $redis; } /** * Get Key Name * * @param $keyName * @return string */ protected function getKeyName($keyName) { return $this->_prefix . $keyName; } } 内,您经常调用drawHands,然后依次依次penUpmoveTopenDown。因此,您可以将所有内容放入一个函数中,然后在moveForward内调用该函数:

drawHands

如果传递给const upMoveDownLength = (x, y, length) => { penUp(); moveTo(x, y); penDown(); moveForward(length); }; function drawHands () { penColor("black"); penWidth(3); turnTo(0); upMoveDownLength(215, 280, 15); upMoveDownLength(230, 280, 15); upMoveDownLength(265, 280, 15); upMoveDownLength(280, 280, 15); } 的参数始终为15,则moveForward不需要第三个参数,您可以硬编码upMoveDownLength而不是{{1 }}。

答案 1 :(得分:0)

以下是使用恰好 for循环和所述功能的答案

function myFunction() {
  penUp();
  moveTo(+arguments[1], 280);
  if (+arguments[0] === 0) {
    penColor("black");
  }
  penDown();
  if (+arguments[0] === 0) {
    penWidth(3);
    turnTo(0);
  }
  moveForward(15);
}

function drawHands () {
  for (var i = 0; i < 4; i++) {
    myFunction(i, [215, 230, 265, 280][i])
  }
}
drawHands();

如果可以在第一个“移动”之前设置penColor,penWidth和turnTo,则可以简化此操作

function myFunction() {
  penUp();
  moveTo(+arguments[0], 280);
  penDown();
  moveForward(15);
}

function drawHands () {
  penColor("black");
  penWidth(3);
  turnTo(0);
  for (var i = 0; i < 4; i++) {
    myFunction([215, 230, 265, 280][i])
  }
}
drawHands();

myFunction([215, 230, 265, 280][i])用内联数组中的第i个值调用myFuction ... 215、230等

现在,由于myFunction(如您所描述的那样不能具有形式参数),因此代码使用函数arguments“属性”来检索参数...一元{{1 }}将值强制为数字