0

How to call any javascript function whenever object is created.

well i am not sure but, is there any way to call javascript function when i create any object.?

var myScroll = new iScroll("wrapper");

function objectCreated() {
    alert("yes created")
}

here i want to call function objectCreated function when i create iScroll object. i know i can call function objectCreated from iScroll constructor but i dont want to make change in iscroll.

Sunil Dodiya
  • 2,605
  • 2
  • 18
  • 21

2 Answers2

1

You can't intercept any object creation, but you can patch iScroll in this case:

// overwrite with a patched function
iScroll = (function(old) {
  return function() {
    // call your interceptor function
    objectCreated();

    // pass everything through to the original iScroll function
    return old.apply(this, arguments);
  };
})(iScroll);
pimvdb
  • 151,816
  • 78
  • 307
  • 352
0

I'll add a new answer. I patched the iScroll (I'll make a pull request for cubiq later). Demo can be seen here: http://www.hakoniemi.net/labs/iscroll/ and the patched iScroll can be found here: https://github.com/zvona/iscroll/blob/master/src/iscroll.js

In brief the code looks like this:

myIScroll = new iScroll("myScroll", {
    "onCreate" : function() { alert('created'); },
    "onDestroy" : function() { alert('destroyed'); }
});
Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74