我正在嘗試使我的對象列表中允許的選項的預設列表。這裏是代碼在對象中預設選項並查看它是否不存在於允許的屬性中
var a = function(cmd, options){
var objList = [options.search ,options.demand];
if(!(options in objList)){
console.warn('Not an Allowed * in the options Property');
}
};
,或者我應該做的
var a = function(cmd, options){
var objList = [search , demand];
if(!(options in objList)){
console.warn('Not an Allowed option in the options Property');
}
};
基本上就是我想要做的是設置search
和demand
被允許的選項中選擇屬性,以便最遲可以做
a('cmd',{
search:'',
demand:function() {
alert('Hello');
},
//warn that the next option is not allowed
quote: function() {
alert('quote of user');
}
});
如果您無法理解我所問的問題,請提問,我會盡我所能解釋更多。
也許寫這樣會更好?
var a = function(cmd, options){
options = {
theme: function(color) {
$('body').css('backgroundColor',color);
},
color:''
};
};
a('cmd',{
theme:'#000'//though this is not working?
});
爲什麼你需要這個?將參數傳遞給對象的想法是,它允許您選擇要在函數中使用哪個參數,您可以簡單地忽略'options'對象中的額外屬性。 – Teemu
那麼,因爲我不想使用額外的選項。順便說一句,我是新來的東西。我通常使用多個對象符號來創建我需要的代碼,但我想創建一個深度對象。也許我可以用我的代碼做一些不同的事情?通過可能使得'options = {//更深入的對象列表在這裏?\\};'以便在代碼內部執行'('cmd',{theme:'',color:''等) );'?? – EasyBB
這個......你只能使用你所需要的'options'的屬性,如果有額外的屬性,它們在'a'執行後被垃圾回收。你編輯的例子不起作用,因爲你重寫了'options'參數。 – Teemu