在 jquery 插件选项中,您经常会看到 true 和 false 像这样使用:
var defaults = {
one: 'false',
two: 'true',
three: 'true'
};
我想知道我是否可以做这样的事情:
var defaults = {
doStuff: 'three two' // order of these wouldn't matter
};
上面的代码等于:
one == false
two == true
three == true
问题是,我怎样才能实现这一目标?
最佳答案
也许你想要一些东西like this
(function($){
var defaults = {
flags : ""
}
$.myPlugin = function(options){
options = $.extend(true, {}, defaults, options);
$.each(options.flags.split(" "), function(i, v){
options[v] = true;
});
if (options.one) {
alert("one setted");
}
if (!options.three) {
alert("three not set");
}
}
})(jQuery);
$.myPlugin({flags: "one two"});
关于javascript - 如何使 jquery 插件选项 true/false 变成更紧凑的形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9411102/