一般我在写一个函数的时候,可能需要一个回调函数,例如:
function loadQtipCode(dom, title, content, width, showcb, hidecb) { $(dom).qtip({ content: { title: { text: title, button: true }, text: content }, position: { my: ‘center‘, at: ‘center‘, target: $(window) }, show: { event: false, solo: false, ready: true, modal: { on: true, blur: false } }, hide: { event: "unfocus" }, events: { hide: hidecb || false, show: showcb || false }, style: { classes: "qtip-shadow qtip-bootstrap qtip-service", height: false, width: width } }); }
if(hidecb ){ return false ; }else{ return hidecb; }
(function(){ var a =hidecb() ; if(new Boolean(a)){ return a; }else{ return showcb(); } }());
前者是作为构造函数构造一个Boolean实例,得到的是一个对象,后者是作为普通函数调用,得到的是函数返回值false。
new Boolean(null); 输出: Boolean {[[PrimitiveValue]]: false}
Boolean(null) 输出:false
在项目中那个少用if else 语句,精简代码,便于维护的方法(1)
原文:http://www.cnblogs.com/laneyfu/p/4360209.html