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
  • 10,772
  • 22
  • 72
  • 132

2 Answers2

3
newClass().defer

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

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
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
  • 14,821
  • 4
  • 34
  • 35