[英]A function uses two parameters: a variable and a string with the same name. Reduce them to one parameter?
I created a function to make it easy to add animations to any element with a set of key parameters such as: animation-name and animation-duration , etc. Here is the function below: 我创建了一个函数,可以使用一组关键参数轻松地将动画添加到任何元素,例如: animation-name和animation-duration等。这是下面的函数:
addAnimation( h1, 'blur', '50s', '0s', 'ease' );
My question has to do with a smaller function used in the former. 我的问题与前者中使用的较小函数有关。 It updates the animation properties and takes 3 parameters two of which have the same exact name, only 1 is a variable and 1 is a string :
它更新动画属性,并接受3个参数,其中两个具有相同的确切名称,只有1个是变量,而1个是字符串 :
updateAnimationProperties( element, 'name', name );
updateAnimationProperties( element, 'duration', duration );
updateAnimationProperties( element, 'delay', delay );
updateAnimationProperties( element, 'timing', timing );
updateAnimationProperties( element, 'fill', fill );
See the redundancy? 看到冗余了吗?
'name', name
, 'duration', duration
, etc. I know this isn't a major problem most developers will worry about but I'm curious to learn the ins and outs of what JavaScript can do. 'name', name
'duration', duration
, 'duration', duration
等。我知道这并不是大多数开发人员都会担心的主要问题,但我很好奇了解JavaScript可以做些什么。 Is there a way to reduce my updateAnimationProperties
function to only use 2 parameters total by reducing the 2 repetitive ones into one? 有没有一种方法可以将我的
updateAnimationProperties
函数减少到2个重复参数,从而只使用2个参数呢?
Full code below 完整代码如下
//\\ \\ \\ \\ \\ \\ \\ UPDATE ANIMATION PROPERTIES / / / / / / / / / / / / / / / / / / function updateAnimationProperties( element, property, value ){ var style = element.style, property = 'animation' + property.charAt( 0 ).toUpperCase() + property.slice( 1 ); if( style[ property ] === '' ){ style[ property ] = value; } else { style[ property ] += ', ' + value; } } /// / / / / / / UPDATE ANIMATION PROPERTIES \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ //\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ ADD ANIMATION / / / / / / / / / / / / / / / / / / / / / / function addAnimation( element, name, duration, delay, timing, fill ){ element.classList.add( 'animation' ); updateAnimationProperties( element, 'name', name ); updateAnimationProperties( element, 'duration', duration ); //default = 1s updateAnimationProperties( element, 'delay', delay ); //default = 0s updateAnimationProperties( element, 'timing', timing ); //default = ease updateAnimationProperties( element, 'fill', fill ); //default = forwards); } /// / / / / / / / / / ADD ANIMATION \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ var h1 = document.querySelector( 'h1' ); addAnimation( h1, 'blur', '50s', '0s', 'ease' ); addAnimation( h1, 'fade', '2s', '1s', 'ease' ); addAnimation( h1, 'shrink', '20s', '0s', 'ease' );
/* \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ ANIMATION DEFINITIONS / / / / / / / / / / / / / */ .animation { animation-duration: 1s; animation-delay: 0s; animation-iteration-count: 1; animation-timing-function: ease; animation-direction: normal; animation-fill-mode: forwards; animation-play-state: running; } .blur { animation-name: blur; } .fade { animation-name: fade; } .shrink { animation-name: shrink; } /*\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ ANIMATION KEYFRAMES / / / / / / / / / / / / / /*/ @keyframes blur { 100% { filter: blur( 5rem ); } } @keyframes fade { 100% { opacity: 0; } } @keyframes shrink { 100% { transform: scale( 0 ); } }
<h1 style="display: inline-block; font-family: sans-seirf; font-weight: normal"> Blur me, fade me, and shrink me </h1>
RECAP 概括
In functions like this: 在这样的函数中:
someFunction( 'value', value )
how can I turn this into someFunction( 'value', value )
如何将其转换为
someFunction( value )
or someFunction( 'value' )
. someFunction( value )
或someFunction( 'value' )
。
Specifically in relation to my code above? 特别是与我上面的代码有关?
Using ES6 Object Literal Property Value Shorthand you can have the function accept an object and then the call would look like updateAnimationProperties( element, {name});
使用ES6 对象文字属性值速记,您可以让函数接受一个对象,然后调用看起来像
updateAnimationProperties( element, {name});
then in the function you can get the object key and value 然后在函数中,您可以获得对象的键和值
function updateAnimationProperties(element, obj) {
let key = Object.keys(obj)[0];
let value = obj[key];
// whatever else the function does with the values
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.