简体   繁体   English

编写这个javascript函数的正确方法是什么?

[英]What is the correct way to write this javascript function?

I'm trying to understand javascript and javascript functions. 我正在尝试理解javascript和javascript函数。 This function is used to change the look of a button. 此功能用于更改按钮的外观。

function changeButton(){
    btn.style.backgroundColor = btn.style.backgroundColor == "black" ? "white" : "black";
    btn.style.color = btn.style.color == "white" ? "black" : "white";
    btn.innerHTML = btn.innerHTML == 'GOOD JOB' ? 'CLICK ME' : 'GOOD JOB';
}

It works just fine. 它工作得很好。 But when I look at this function I see a lot of repetition. 但是当我看到这个功能时,我看到了很多重复。 The basic structure seems to be element = element == value1 ? value2 : value1 基本结构似乎是element = element == value1 ? value2 : value1 element = element == value1 ? value2 : value1

So in my mind this should work: 所以我认为这应该有效:

function toggleValue(elem, val1, val2){
    elem = elem == val1 ? val2 : val1
}

function changeButton(){
    var x = btn.style.backgroundColor
    var y = btn.style.color
    var z = btn.innerHTML
    toggleValue(x, 'white', 'black')
    toggleValue(y, 'black', 'white')
    toggleValue(z, 'CLICK ME', 'GOOD JOB')
}

But it does not work and I don't understand why. 但它不起作用,我不明白为什么。 Can someone tell me why this doesn't work? 有人能告诉我为什么这不起作用? And is there a way to make it work? 有没有办法让它发挥作用?

It does not work, because you hand over a primitive value an not a reference to the property. 它不起作用,因为您移交原始值而不是对属性的引用。

function toggleValue(elem, prop, val1, val2){
    elem[prop] = elem[prop] == val1 ? val2 : val1
}

function changeButton(){
    toggleValue(btn.style, 'backgroundColor', 'white', 'black')
    toggleValue(btn.style, 'color', 'black', 'white')
    toggleValue(btn, 'innerHTML', 'CLICK ME', 'GOOD JOB')
}

Because you are passing the String which is immutable . 因为您传递的是不可变String

You can create one more argument - property . 您可以再创建一个参数 - property

function toggleValue(elem, prop, val1, val2){
    elem[prop] = elem[prop] == val1 ? val2 : val1
}

function changeButton(){
    var x = btn.style;
    var y = btn.style;
    var z = btn;
    toggleValue(x, "backgroundColor", 'white', 'black');
    toggleValue(y, "color", 'black', 'white');
    toggleValue(z, "innerHTML", 'CLICK ME', 'GOOD JOB');
}

 function toggleValue(elem, val1, val2, ButtonStyleType) { elem = elem == val1 ? val2 : val1; if (ButtonStyleType == 'backgroundColor') btn.style.backgroundColor = elem; else if (ButtonStyleType == 'color') btn.style.color = elem; else if (ButtonStyleType == 'innerHtml') btn.innerHTML = elem; } function changeButton() { var x = btn.style.backgroundColor; var y = btn.style.color; var z = btn.innerHTML; toggleValue(x, 'green', 'yellow', 'backgroundColor'); toggleValue(y, 'black', 'Orange', 'color'); toggleValue(z, 'Default', 'GOOD JOB', 'innerHtml'); } 
 <button id="btn" style="background-color:green; color:black;">Default</button><br /> <br /> <a style="cursor:pointer;color:blue;" onclick="changeButton();">Click here to update button style</a> 

您必须了解传递值与传递参考之间的区别, 以了解有关该点击的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM