見JavaScript註釋JavaScript設置變量導致或沒有
var SearchResult = {
googleApiKey: "",
googleUrl: "https://www.googleapis.com/shopping/search/v1/public/products?key={key}&country={country}&q={query}&alt=atom",
country: "UK"
Query: function(args)
{
// Is there a way to do this in a less messy way?
args.googleApiKey ? : this.googleApiKey = args.googleApiKey : null;
args.country? : this.country = args.country: null;
}
}
基本上,如果有人對我的對象屬性提供了一個新的價值,我想它來設置它,否則只是繼續使用提供的缺省值。
我知道位運算符是選擇選擇的好選擇,但我不知道如何將它轉換爲javascript?
你做錯了。你的意思是'args.googleApiKey = this.googleApiKey? this.googleApiKey:null;'或使用||如下面的答案所示 – mplungjan 2013-02-13 10:49:18