0

Ok so, I am trying to access a property and I am having issues with it. The issue seems to be while accessing this.threshhold. In the example below, I expected the alert to display 100, but I am getting undefined.

I understand that the object has not yet been created and I assume that neither the object nor the property exist yet(?) so the object cannot not be accessed for that obvious reason.

Am I approaching this wrongly? Can this be done? If not; is there any other clean/proper way to do this?

Hopefully, I have been clear enough.

https://jsfiddle.net/fq58vz1f/

  var helpers = {
    throttle: function(fn, threshhold) {
      return function() {
        alert(threshhold);
      }
    }
  };

  var header = {
    threshhold: 100,
    scroll: helpers.throttle(function() {
      // stuff
    }, this.threshhold)
  };

  header.scroll();
human
  • 686
  • 3
  • 9
  • 24
  • 3
    You can't. `this` has meaning inside functions, otherwise it defaults to `window` or `undefined` in strict mode – elclanrs Mar 17 '16 at 17:27
  • `this` is a keyword that in OOP reffers to an instantiated object. You haven't got any instances (you have a literal object, that's not the same, nor OOP), and you can't use `this` because it reffers to window object – Marcos Pérez Gude Mar 17 '16 at 17:28
  • You can't reference the object inside an object initializer, because it has not been created yet. – Oriol Mar 17 '16 at 17:28
  • 1
    @Oriol: Well, that's a somewhat philosophical point. By the time that the `scroll` property initializer above is being processed, there is definitely an object, and that object definitely has a `threshold` property, because the spec requires that property initializers be executed in order. We just don't have (and can't have) any reference to that object yet. :-) (And I suppose it would be possible for an engine not to create the object, just to keep a list of the properties for it until later. Seems awkward, but it would be possible.) – T.J. Crowder Mar 17 '16 at 17:29
  • @T.J.Crowder if we see this code in another language, maybe we call static methods, so there's no instance, so there's no "object" per-se, so there's no possibility to use `this` as you said as refference. Remember that an object is an instance of a class, except in javascript that we call all objects even with risk of mistakes – Marcos Pérez Gude Mar 17 '16 at 17:32
  • you can use a getter in object literals to reach "sibling" properties. – dandavis Mar 17 '16 at 17:33
  • Ok, so then it seems that I got confused regarding the scope of the `threshhold` property. I assumed the property was being accessed from within the `header` scope, but I now understand it is being accessed from the `window` scope. Could `scroll: helpers.throttle(function() {` somehow be changed in a way that will allow me to pass the property / how would you approach this? – human Mar 17 '16 at 17:39
  • @MarcosPérezGude: Objects are only instances of classes in class-based OOP. JavaScript doesn't use class-based OOP. – T.J. Crowder Mar 17 '16 at 17:39
  • @human: The answers in the linked question tell you how to solve this. – T.J. Crowder Mar 17 '16 at 17:39
  • @dandavis getters seem interesting. – human Mar 17 '16 at 17:43
  • @T.J.Crowder I am trying to make sense of the answers in there and apply them in my case. – human Mar 17 '16 at 17:45
  • @human: Not to be partisan, but I'd think [my answer](http://stackoverflow.com/a/10766107/157247) would be fairly clear? `var header = {threshold: 100}; header.scroll = helpers.throttle(function() { /* stuff */}, header.threshhold);` – T.J. Crowder Mar 17 '16 at 17:52
  • @T.J.Crowder You answer makes perfect sense, however I am trying to determine if that way would work within my context as I am using a `ready : function(){ this = helpers.apply_filters( 'header_args', this ); })` inside the `header` object, this function is being called when the page is ready and `apply_filters` allows me to modularize my app and amend properties from the outside, one of this properties being `threshhold`, could I still use your approach in this case? – human Mar 17 '16 at 18:22
  • @human: It doesn't make any difference. – T.J. Crowder Mar 18 '16 at 06:53

0 Answers0