2015-06-03 146 views
-1

我有下面的构造:js函数将多个参数传递作为单个对象

var one = new HB.hideShowFacilites.Selectors(".facilities .more-centered", "more-centered", "less-centered", "container-max-height", ".facilities .container-min-height"); 

有没有经过所有这些选择的作为单个对象的一种方式?

HB.hideShowFacilites = HB.hideShowFacilites || {}; 

HB.hideShowFacilites.Selectors = function(sel1, sel2, sel3, sel4, sel5){ 
    this.sel1 = sel1; 
    this.sel2 = sel2; 
    this.sel3 = sel3; 
    this.sel4 = sel4; 
    this.sel5 = sel5; 
}; 

HB.hideShowFacilites.Selectors.prototype.hideShow = function(){ 
    var $obj1 = $(this.sel1), 
     $obj2 = this.sel2, 
     $obj3 = this.sel3; 
     $obj4 = this.sel4, 
     $obj5 = $(this.sel5); 

    $obj1.on('click', function(e){ 
     e.preventDefault(); 

     if($obj1.hasClass($obj2)){ 
      $obj1.removeClass($obj2).addClass($obj3); 
      $obj5.addClass($obj4); 
     } 
     else{ 
      $obj1.removeClass($obj3).addClass($obj2); 
      $obj5.removeClass($obj4); 
     }  
    }); 

}; 

$(document).ready(function(){ 

    var one = new HB.hideShowFacilites.Selectors(".facilities .more-centered", "more-centered", "less-centered", "container-max-height", ".facilities .container-min-height"); 

    one.hideShow(); 
}); 
+1

发送作为数组'[ “.facilities更多为中心”, “多中心”, “少为中心”,“CONTA iner-max-height“,”.facilities .container-min-height“]' – Dhiraj

+0

什么是HB.hideShowFacilites.Selectors()查找? – stackoverfloweth

+0

不够清楚,你是否想将值作为一个对象传递给函数,还是使用传入的参数,因为它们是一个对象,或者两者都传递和处理参数,因为它们是一个对象? – skazska

回答

1

它是纯JS不可能在功能上通过与参数列表与构件被当作参数的对象,而无需修改函数是这样的:

HB.hideShowFacilites.Selectors = function(selectors){ 
    this.sel1 = selectors.sel1; 
    this.sel2 = selectors.sel2; 
    this.sel3 = selectors.sel3; 
    this.sel4 = selectors.sel4; 
    this.sel5 = selectors.sel5; 
}; 

函数这样期待一个参数,并把它作为具有sel1,sel2等字段的对象。

但相反也可以使用传入的参数列表作为数组像这样的函数内部:

HB.hideShowFacilites.Selectors = function(sel1, sel2, sel3, sel4, sel5){ 
    this.sel1 = arguments[0]; 
    this.sel2 = arguments[1]; 
    this.sel3 = arguments[2]; 
    this.sel4 = arguments[3]; 
    this.sel5 = arguments[4]; 
}; 

futhermore,如果你不喜欢修改功能,可以使用类似的重新定义它此

HB.myHideShowFacilites = function(){}; 

HB.myHideShowFacilites.prototype = HB.hideShowFacilites; 

HB.hideShowFacilites.Selectors = function(selectors){ 
    this.sel1 = selectors.sel1; 
    this.sel2 = selectors.sel2; 
    this.sel3 = selectors.sel3; 
    this.sel4 = selectors.sel4; 
    this.sel5 = selectors.sel5; 
}; 

,然后使用HB.myHideShowFacilites代替HB.hideShowFacilites

2

根据HB.hideShowFacilites.Selectors是如何实现的,你可以使用Function.prototype.apply这样

function foo(args) { 
    var instance = Object.create(HB.hideShowFacilites.Selectors.prototype); 
    HB.hideShowFacilites.Selectors.apply(instance, args); 
    return instance; 
} 

var one = foo([".facilities .more-centered", "more-centered", "less-centered", "container-max-height", ".facilities .container-min-height"]); 

从你它是如何定义的修改,这种方法应该工作。

+0

我觉得这太复杂了..有没有一种方法来定义一个对象与选择器,并将该对象作为参数传递给“var one = new HB.hideShowFacilites.Selectors”? – Alex

相关问题