【发布时间】:2015-10-16 01:50:10
【问题描述】:
我知道有一些与此非常相似的问题,但我似乎无法解决任何问题。
为了简洁起见,我将对此进行总结。我有这个构造函数,字符串被分配为键的值(赋值)。
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};
};
【问题讨论】:
标签: javascript function constructor key