2

So I have this:

(function () {
   // work
}).defer(1000);

For my admittedly weak understanding of javascript this executes an anonymous class after a second of wait time.

So I wanted to change it to something like this:

var newClass = function() {
   // work
};
newClass().defer(1000);

But this doesn't give me the delay for some reason. I feel like these two code samples are exactly the same, I am wrong in this assumption?

Edit:

I'm using this defer method, sorry I thought it was a javascript thing.

Grammin
  • 11,224
  • 22
  • 76
  • 135

2 Answers2

3
newClass().defer

This calls the function, then calls defer() on whatever the function returns.

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
2

you don't define a defer method, so here is one that works (almost) like you expect:

Function.prototype.defer=function(delay){ return setTimeout(this, delay || 100); };
var newClass = function() {
   alert ("working");
   // work
};
newClass.defer(1000);
dandavis
  • 15,617
  • 5
  • 38
  • 36