简体   繁体   English

函数使用两个参数:变量和具有相同名称的字符串。 将它们减少为一个参数?

[英]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-nameanimation-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.

相关问题 函数变量和函数变量具有相同的名称。 如何进入? - function variable and in function variable have the same name. How to access? 将字符串转换为变量名称。 (JavaScript的) - Converting string to variable name. (JavaScript) 如何使用类名仅在一个元素上运行jquery函数。 有更多的元素使用相同的类 - how to run jquery function only on one element using class name. there is more element using same class 将变量分配为具有相同名称的 function 参数 - Assign a variable as a function parameter with the same name 为什么不能用 let 声明一个和 function 参数同名的变量,连参数都设置默认值? - Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value? 如何将一个参数的数据传递给具有两个参数的 function? - How to pass data to one parameter to a function with two parameters? 函数内部具有相同名称的变量将更改所传递参数的值 - Variable with same name inside function changes the value of the passed parameter 如何使用函数参数来引用同名变量? - How to use a function parameter to refer to the variable of the same name? 不能使用 let 定义与函数参数同名的变量 - Cannot use let to define a variable with the same name of function's parameter JavaScript:与其他变量同名的回调函数参数? - JavaScript: callback function parameter with same name as other variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM