2015-06-03 186 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

相關問題