如何将4个相同的函数组合成一个可以重用的新函数

时间:2016-11-04 08:49:34

标签: javascript function

我做了一些功能,但他们都做了大致相同的事情。 我想将这些功能组合成一个可以重复使用的新功能,以防我需要一个像这样的新功能..是否可以这样做或者我只是在浪费你和我的时间?

以下是代码:

function getBinding(selectedBinding) {
  var b = elems.binding;
  var selectedBinding = b.options[b.selectedIndex].value;
  return selectedBinding;
} 

// Getting the holes value
function getHoles(selectedHoles) {
  var h = elems.holes;
  var selectedHoles = h.options[h.selectedIndex].value;
  return selectedHoles;
}

// Getting the paper weight value
function getPaperWeight(selectedPaperWeight) {
  var pW = elems.paperWeight;
  var selectedPaperWeight = pW.options[pW.selectedIndex].value;
  return selectedPaperWeight;
}

// Getting the staples value
function getStaples(selectedStapling) {
  var s = elems.stapling;
  var selectedStapling = s.options[s.selectedIndex].value;
  return selectedStapling;
}

有什么建议吗?

非常感谢!

7 个答案:

答案 0 :(得分:0)

getByKey

之类的东西



// 
function getValueByKey(key) {
  var elem = elems[key];
  var optionValue = elem.options[elem.selectedIndex].value;
  return optionValue;
}




答案 1 :(得分:0)

我会将prop类型作为参数 -

function selectByPropAndIndex(prop){
    var elemByProp = elems[prop];
    var selected= elemByProp.options[elemByProp.selectedIndex].value;
    return selected;
}

答案 2 :(得分:0)

您可以将元素作为参数。

func showAlert()
    {
        let titleStr = "title"
        let messageStr = "message"

        let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertControllerStyle.alert)

        let placeholderStr =  "placeholder"

        alert.addTextField(configurationHandler: {(textField: UITextField) in
            textField.placeholder = placeholderStr
            textField.addTarget(self, action: #selector(self.textChanged(_:)), for: .editingChanged)
        })

        let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (_) -> Void in

        })

        let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { (_) -> Void in
            let textfield = alert.textFields!.first!

            //Do what you want with the textfield!
        })

        alert.addAction(cancel)
        alert.addAction(action)

        self.actionToEnable = action
        action.isEnabled = false
        self.present(alert, animated: true, completion: nil)
    }

    func textChanged(_ sender:UITextField) {
        self.actionToEnable?.isEnabled  = (sender.text! == "Validation")
    }

答案 3 :(得分:0)

您可以使用partial application创建此类功能:

function getItems(elementType) {
  return function(elements) {
    var items = elements[elementType];
    return items.options[items.selectedIndex].value;
  };
}

var getBinding = getItems('binding');
var getHoles = getItems('holes');
var getPaperWeight = getItems('paperWeight');
var getStaples = getItems('stapling');

用法:

getBinding(elems);

答案 4 :(得分:0)

你可以试试这个:

function getSelectedProperty(property) {
    var elem = elems[property];
    return elem.options[elem.selectedIndex].value;
}

另请注意,您的原始代码存在一些问题。这些参数是无用的,因为您在每个函数中定义了一个新变量,该变量将覆盖参数名称,因为您给它指定了相同的名称。举个例子:

function foo(x) {
    var x = 5; // this x is not the same as the parameter x
}

答案 5 :(得分:0)

function getBinding(selectedElement, type) {
    switch(type) {
        case binding:
            var element = elems.binding;
            break;
        case hole:
            var element = elems.holes;
            break;
        case paper:
            var element = elems.paperWeight;
            break;
        case staples:
            var element = elems.stapling;
            break;
    }
    var selectedElement = element.options[element.selectedIndex].value;
    return selectedElement;
} 

答案 6 :(得分:0)

这是我修复它的方式!

function getValue(selector){ 
    return parseInt(selector.options[selector.selectedIndex].value);
};

全部谢谢!