【问题标题】:Accessing value of key inside object访问对象内键的值
【发布时间】: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


    【解决方案1】:

    在你的函数中,你应该可以使用这个键来访问引用:

    object[randomProp]
    

    在 JavaScript 中,可以使用方括号表示法访问对象属性。见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

    【讨论】:

      【解决方案2】:

      如您所料,您的代码有几处问题:)

      我已经用相当彻底的 cmets 重写了它。

      <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>
      

      【讨论】:

        【解决方案3】:

        您应该使用return object[randomProp],而不是return { quote: randomProp }

        您可以通过点表示法或方括号表示法访问对象属性。

        这是一个来自 jsbin 的例子 https://jsbin.com/yopeli/edit?js,console

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-04-03
          • 2020-10-06
          • 2020-05-18
          • 2020-10-10
          • 2018-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多