JavaScript Calling methods on objects inside a loop calling methods using objects in java,calling methods on objects

But, if I change gun.heft to guns[gun].heft, I get the correct output.

...

It seems that I can get the name of the gun inside the loop, but I can't call a method on it to get the heft. Why?

There's no code in your question trying to use a method on the objects. If there were, it would work, for the same reason that guns[gun].heft works:

// The function we'll make a method
function getGunHeft() {
    return this.heft;
}

// Create the objects with that function on them as a method
var rockSpearguns = {
  Sharpshooter: {barbs: 2, weight: 10, heft: "overhand", getHeft: getGunHeft},
  Pokepistol: {barbs: 4, weight: 8, heft: "shoulder", getHeft: getGunHeft}
};

// Use the method
function listGuns(guns) {
  for ( var gun in guns ) {
    console.log("Behold! " + gun + ", with "  + guns[gun].getHeft(); + " heft!");
  }
}
listGuns(rockSpearguns);

"Methods" in JavaScript are just functions attached to object properties (either directly, as above, or through the object's underlying "prototype" object). So if you can access an object property, you can call the function it refers to.

When you call a function as part of the same expression where you get the function from an object property, the JavaScript engine sets this to refer to the object during the call to the function. That's why I used this.heft above to access the heft property of the gun.

More in this article on my blog: Mythical Methods

Answer:1

When you're navigating through an object with a for-each loop, you have to use guns[gun] to access said object, presuming you're object is guns and you're looping through to find gun.

Answer:2

gun here would be the index of your loop. Try:

function listGuns(guns) {
  for ( var gun in guns ) {
    console.log("Behold! " + gun + ", with "  + guns[gun].heft + " heft!");
  }
}
Answer:3

Since you're relating the loop to Ruby, I'll show the comparison:

JS:

for (gun in guns)

is analogous to Ruby:

guns.keys.each do |gun|

In both cases, gun is set to the property name, not the property value. To get the corresponding value, you have to access guns[gun] in both languages.

In Ruby you would probably write the loop as:

guns.each do |gunname, gun|

so that you can access properties of the gun object directly. Javascript doesn't have this operation built in, but it's simply to write a function that does it.

Answer:4

I'm learning AngularJS and I have a bit of a problem with directives. I don't like how they look. Specifically, I don't like when there is a lot of html in the middle of the javascript. Look at the ...

I'm learning AngularJS and I have a bit of a problem with directives. I don't like how they look. Specifically, I don't like when there is a lot of html in the middle of the javascript. Look at the ...

  1. angularjs directive without template
  2. angularjs directive without controller
  3. angularjs directive without isolated scope
  4. angularjs directive without scope
  5. angularjs directives not working
  6. angularjs directive not rendering
  7. angularjs directive not loading
  8. angularjs directive not showing
  9. angularjs directive not updating
  10. angularjs directive not being called
  11. angularjs directive not isolated scope
  12. angularjs directive not refreshing
  13. angularjs directive not updating parent scope
  14. angularjs directive not showing up
  15. angularjs directive not updating scope
  16. angularjs directive attribute without value

I'm trying to create a combobox to show moths, when I do click on january I only need to see div1 and febraury need to see div 2 like this: January........ <div> It will show info when ...

I'm trying to create a combobox to show moths, when I do click on january I only need to see div1 and febraury need to see div 2 like this: January........ <div> It will show info when ...

  1. javascript create and show div
  2. how to create a slideshow in javascript
  3. javascript createelement show
  4. create image slideshow in javascript

Everything works except for the replacement of the div. When i submit the form with jquery and ajax, my php stores the data in the database and it creates a new div that needs to be placed where the ...

Everything works except for the replacement of the div. When i submit the form with jquery and ajax, my php stores the data in the database and it creates a new div that needs to be placed where the ...

  1. with data in sql
  2. with data in r
  3. with data in excel
  4. with data of type
  5. curl with data from file
  6. with in data.table
  7. with in data.table r
  8. with in data bind
  9. curl post with data from file

I'm trying to display full month name like 'January 2014' with eyecon Bootstrap datepicker http://www.eyecon.ro/bootstrap-datepicker/ Does anybody know if it's possible? I've tried everything I know (...

I'm trying to display full month name like 'January 2014' with eyecon Bootstrap datepicker http://www.eyecon.ro/bootstrap-datepicker/ Does anybody know if it's possible? I've tried everything I know (...

  1. displayport
  2. display case
  3. display synonym
  4. display cabinet
  5. displayport cable
  6. display driver uninstaller
  7. displayport to hdmi
  8. display fusion
  9. display flex
  10. display shelves
  11. displayport vs hdmi
  12. display css
  13. display board
  14. displaylink
  15. display definition
  16. display shelf
  17. display stand
  18. display settings
  19. display cable
  20. display box