我知道有一些極其類似的問題,但我似乎無法得到任何工作。訪問對象內的鍵的值
爲了簡潔起見,我將對此進行總結。我有這個構造函數的字符串被賦值爲鍵值(賦值)。
var Quote = function() {
this.quote1 = 'You can discover more about a person in an hour of play than in a year of conversation.';
this.quote2 = 'Nothing is at last sacred but the integrity of your own mind.';
this.quote3 = 'We have to fight them daily, like fleas, those many small worries about the morrow, for they sap our energies.';
this.quote4 = 'Ethics are so annoying. I avoid them on principle.';
this.quote5 = "Never trust anything that can think for itself if you can't see where it keeps its brain.";
};
module.exports = Quote;
使用AJAX我將構造函數中鍵的值打印到靜態頁面。不過......我只能打印「quote1」,「quote2」等等......(只是鍵的名字)。
這是我訪問構造函數的函數。我的問題是:如何訪問分配給構造函數中的鍵的字符串值?那可能嗎?
預先感謝您。
module.exports = function(object) {
var propArray = Object.keys(object);
var randomProp = propArray[Math.floor(Math.random() * propArray.length)];
return {quote: randomProp};
};
非常感謝您的例子! – Jose