javascript - 这个闭包我想传多个参数进去该怎么改进?

查看:90
本文介绍了javascript - 这个闭包我想传多个参数进去该怎么改进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

    var cssSupports = (function() {
    var div = document.getElementById('cultural-banner'),
        vendors = 'Khtml O Moz Webkit'.split(' '),
        len = vendors.length;
    return function(prop) {
        if ( prop in div.style ) return true;
        if ('-ms-' + prop in div.style) return true;
        
        prop = prop.replace(/^[a-z]/, function(val) {
            return val.toUpperCase();
        });

        while(len--) {
            if ( vendors[len] + prop in div.style ) {
            return true;
        }
    }
        return false;
    };
})();
console.log(cssSupports('transition'));

想要实现的效果就是cssSupports(elem,property)在里面传一个选择器和一个属性名,该怎么改?

解决方案

直接这样不好么?

var cssSupports = function(selector, property) {
    var selected = document.querySelector(selector),
        vendors = 'Khtml O Moz Webkit'.split(' '),
        len = vendors.length;
    if (property in selected.style) {
        return true;
    }
    if ('-ms-' + property in selected.style) {
        return true;
    }

    property = property.replace(/^[a-z]/, function(val) {
        return val.toUpperCase();
    });

    while (len--) {
        if (vendors[len] + property in selected.style) {
            return true;
        }
    }
    return false;
};
console.log(cssSupports('#cultural-banner', 'transition'));

这篇关于javascript - 这个闭包我想传多个参数进去该怎么改进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆