Javascript 访问对象内部的键的值

Javascript 访问对象内部的键的值,javascript,function,constructor,key,Javascript,Function,Constructor,Key,我知道,有几个问题与此极其相似,但我似乎什么都做不到 为了简洁起见,我将对此进行总结。我有一个构造函数,其中字符串被指定为键的值(赋值) 使用AJAX,我将构造函数中键的值打印到一个静态页面。然而。。。我只成功地打印了“quote1”、“quote2”等。。。(只是钥匙的名字) 这就是我访问构造函数的函数的样子。我的问题是:如何访问分配给构造函数中键的字符串值?可能吗 先谢谢你 module.exports = function(object) { var propArray = Objec

我知道,有几个问题与此极其相似,但我似乎什么都做不到

为了简洁起见,我将对此进行总结。我有一个构造函数,其中字符串被指定为键的值(赋值)

使用AJAX,我将构造函数中键的值打印到一个静态页面。然而。。。我只成功地打印了“quote1”、“quote2”等。。。(只是钥匙的名字)

这就是我访问构造函数的函数的样子。我的问题是:如何访问分配给构造函数中键的字符串值?可能吗

先谢谢你

module.exports = function(object) {
  var propArray = Object.keys(object);
  var randomProp = propArray[Math.floor(Math.random() * propArray.length)];
  return {quote: randomProp};
};

在您的函数中,您应该能够使用以下命令通过键访问报价:

object[randomProp]

在JavaScript中,可以使用方括号表示法访问对象属性。请参阅。

在您的函数中,您应该能够使用以下命令通过键访问报价:

object[randomProp]

在JavaScript中,可以使用方括号表示法访问对象属性。请参阅。

正如您可能预期的那样,您的代码有几处错误:)

我用相当透彻的评论重写了它

<script>

// create your 'Quote' class, which can be instantiated as many times as necessary
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.";
};

// now let's set up the 'exports' method on the function's prototype
// this will allow any new instance of the Quote function to call or override this method
// without affecting the master 'Quote' class
Quote.prototype.exports = function() {
    // you don't need to explicitly pass an object to this function
    // just have it reference itself using the 'this' keyword
    var propArray = Object.keys(this);
    var randomProp = propArray[Math.floor(Math.random() * propArray.length)];

    // objects in JavaScript can be handled like associative arrays
    // simply access the key of 'this' with the value provided by 'randomProp'
    return {quote: this[randomProp]};
};

// create a new instance of 'Quote'
// this guy can be used and manipulated independent of the master 'Quote' class
var module = new Quote();
// let's store the value returned by 'exports' in randomQuote
// note the () at the end of module.exports -- this tells JavaScript to treat the object as a function
var randomQuote = module.exports();

// write your quote to the document -- or do whatever else you want to with it at this point
document.write(randomQuote.quote);

</script>

//创建“Quote”类,该类可以根据需要多次实例化
var Quote=function(){
this.quote1=“你可以在一个小时的游戏中发现更多关于一个人的信息,而不是在一年的谈话中。”;
this.quote2=“除了你自己思想的完整性,没有什么是神圣的。”;
这句话的意思是:“我们必须像跳蚤一样每天与它们战斗,因为它们会消耗我们的精力。”;
this.quote4=“道德太烦人了,我原则上避免道德。”;
this.quote5=“如果你看不到任何能独立思考的东西的大脑在哪里,就不要相信它。”;
};
//现在,让我们在函数的原型上设置“导出”方法
//这将允许Quote函数的任何新实例调用或重写此方法
//不影响主“报价”类
Quote.prototype.exports=函数(){
//您不需要显式地将对象传递给此函数
//只要让它使用“this”关键字引用自己就行了
var-propArray=Object.keys(这个);
var randomProp=propArray[Math.floor(Math.random()*propArray.length)];
//JavaScript中的对象可以像关联数组一样处理
//只需使用“randomProp”提供的值访问“this”的键
返回{quote:this[randomProp]};
};
//创建“Quote”的新实例
//这个家伙可以独立于主“引用”类使用和操纵
var模块=新报价();
//让我们将“导出”返回的值存储在randomQuote中
//注意module.exports末尾的()命令——这告诉JavaScript将对象视为函数
var randomQuote=module.exports();
//在文档中写下您的报价,或者在此时对其执行任何您想执行的操作
文件。编写(随机引用。引用);

正如您所预料的,您的代码有几处错误:)

我用相当透彻的评论重写了它

<script>

// create your 'Quote' class, which can be instantiated as many times as necessary
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.";
};

// now let's set up the 'exports' method on the function's prototype
// this will allow any new instance of the Quote function to call or override this method
// without affecting the master 'Quote' class
Quote.prototype.exports = function() {
    // you don't need to explicitly pass an object to this function
    // just have it reference itself using the 'this' keyword
    var propArray = Object.keys(this);
    var randomProp = propArray[Math.floor(Math.random() * propArray.length)];

    // objects in JavaScript can be handled like associative arrays
    // simply access the key of 'this' with the value provided by 'randomProp'
    return {quote: this[randomProp]};
};

// create a new instance of 'Quote'
// this guy can be used and manipulated independent of the master 'Quote' class
var module = new Quote();
// let's store the value returned by 'exports' in randomQuote
// note the () at the end of module.exports -- this tells JavaScript to treat the object as a function
var randomQuote = module.exports();

// write your quote to the document -- or do whatever else you want to with it at this point
document.write(randomQuote.quote);

</script>

//创建“Quote”类,该类可以根据需要多次实例化
var Quote=function(){
this.quote1=“你可以在一个小时的游戏中发现更多关于一个人的信息,而不是在一年的谈话中。”;
this.quote2=“除了你自己思想的完整性,没有什么是神圣的。”;
这句话的意思是:“我们必须像跳蚤一样每天与它们战斗,因为它们会消耗我们的精力。”;
this.quote4=“道德太烦人了,我原则上避免道德。”;
this.quote5=“如果你看不到任何能独立思考的东西的大脑在哪里,就不要相信它。”;
};
//现在,让我们在函数的原型上设置“导出”方法
//这将允许Quote函数的任何新实例调用或重写此方法
//不影响主“报价”类
Quote.prototype.exports=函数(){
//您不需要显式地将对象传递给此函数
//只要让它使用“this”关键字引用自己就行了
var-propArray=Object.keys(这个);
var randomProp=propArray[Math.floor(Math.random()*propArray.length)];
//JavaScript中的对象可以像关联数组一样处理
//只需使用“randomProp”提供的值访问“this”的键
返回{quote:this[randomProp]};
};
//创建“Quote”的新实例
//这个家伙可以独立于主“引用”类使用和操纵
var模块=新报价();
//让我们将“导出”返回的值存储在randomQuote中
//注意module.exports末尾的()命令——这告诉JavaScript将对象视为函数
var randomQuote=module.exports();
//在文档中写下您的报价,或者在此时对其执行任何您想执行的操作
文件。编写(随机引用。引用);

您应该使用
返回对象[randomProp]
而不是
返回{quote:randomProp}

可以通过点符号或方括号符号访问对象属性

这里有一个来自jsbin的示例

您应该使用
返回对象[randomProp]
而不是
返回{quote:randomProp}

可以通过点符号或方括号符号访问对象属性

这里有一个来自jsbin的示例