admin管理员组

文章数量:1426797

There's a handful of questions extremely similar to this, I know, but I can't seem to get anything to work.

For sake of brevity, I'll summarize this. I have this constructor function with strings assigned as values of the keys (assignment).

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;

Using AJAX I am printing the value of the keys inside the constructor to a static page. However... I am only successful in printing "quote1", "quote2", etc... (Just the name of the keys).

This is what my function for accessing the constructor looks like. My question is: How can I access the string values assigned to the keys inside the constructor? Is that possible?

Thank you in advance.

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

There's a handful of questions extremely similar to this, I know, but I can't seem to get anything to work.

For sake of brevity, I'll summarize this. I have this constructor function with strings assigned as values of the keys (assignment).

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;

Using AJAX I am printing the value of the keys inside the constructor to a static page. However... I am only successful in printing "quote1", "quote2", etc... (Just the name of the keys).

This is what my function for accessing the constructor looks like. My question is: How can I access the string values assigned to the keys inside the constructor? Is that possible?

Thank you in advance.

module.exports = function(object) {
  var propArray = Object.keys(object);
  var randomProp = propArray[Math.floor(Math.random() * propArray.length)];
  return {quote: randomProp};
};
Share Improve this question asked Oct 16, 2015 at 1:50 JoseJose 5,2408 gold badges29 silver badges50 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Inside your function, you should be able to access the quote by key using this:

object[randomProp]

In JavaScript, object properties can be accessed using square bracket notation. See https://developer.mozilla/en-US/docs/Web/JavaScript/Guide/Working_with_Objects.

As you might have expected, there are several things wrong with your code :)

I've rewritten it with pretty thorough ments.

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

Instead of return { quote: randomProp } you should use return object[randomProp].

You can access object properties via dot notation or square bracket notation.

Here's an example from jsbin https://jsbin./yopeli/edit?js,console

本文标签: javascriptAccessing value of key inside objectStack Overflow