Loading ...
Sorry, an error occurred while loading the content.
 

DS_JSFunction calling oFunction from global scope

Expand Messages
  • jeremymefford
    I m not quite sure how to fix this problem I m having. I created a custom object that contains an autocomplete widget with a datatable. I used the example off
    Message 1 of 5 , Mar 29, 2008
      I'm not quite sure how to fix this problem I'm having. I created a
      custom object that contains an autocomplete widget with a datatable.

      I used the example off the YUI site that had me use DS_JSFunction for
      my autocomplete widget, however the oFunction that it's calling is
      referenced from the global scope when I want it to be local. I all
      actuality, I'm not even sure how it's calling from the global scope in
      this case.

      Here is my situation, in code:

      var myObj = function() {
      return {

      vars:null,
      init: function() {

      //do stuff
      this.myACDataSource = new YAHOO.widget.DS_JSFunction(this.someFunc);

      }

      someFunc: function() {
      this.myvar = 'foo';
      }

      }
      }

      So, when I debug this w/Firebug with a breakpoint on the line
      this.myvar = 'foo', firebug tells me:

      this = window test.jsp

      I'm so very confused as to how this is the case when clearly the
      function called was in the scope of myObj. I've worked around it by
      calling this.myObjInstance.myvar, but I feel that this is a hack and
      there should be a better way to do this.
    • y_lsmith
      Jeremy, The JavaScript interpreter dereferences this.someFunc to the function itself (not the function scoped to the this object) before passing it to the
      Message 2 of 5 , Mar 31, 2008
        Jeremy,

        The JavaScript interpreter dereferences this.someFunc to the function itself (not the function scoped to the this object) before passing it to the DS_JSFunction constructor.  Functions do not carry their scope with them;  this is determined when they are executing.

        For example:

        var someFunc = function () {
            alert(this.name); // who is this?
        };

        var MyClass = function (name) {
            this.name = name;
            this.fooFunc = someFunc;
        };

        var MyOtherClass = function (name) {
            this.name = name;
        };
        MyOtherClass.prototype.barFunc = someFunc;

        // this is the window object
        someFunc();

        // this is the MyClass instance
        new MyClass('foo').fooFunc();

        // this is the MyOtherClass instance
        new MyOtherClass('bar').barFunc();

        var baz = { name: 'baz' };
        // this is baz
        someFunc.call(baz);

        You can avoid the this confusion by creating a closure around your instance method like so:

        init : function () {
            //do stuff

            var me = this;
            this.myACDataSource = new YAHOO.widget.DS_JSFunction(function () {
                me.someFunc();
            }
        );
        },


        Hope this helps,
        Luke

        --- In ydn-javascript@yahoogroups.com, "jeremymefford" <jeremymefford@...> wrote:
        >
        > I'm not quite sure how to fix this problem I'm having. I created a
        > custom object that contains an autocomplete widget with a datatable.
        >
        > I used the example off the YUI site that had me use DS_JSFunction for
        > my autocomplete widget, however the oFunction that it's calling is
        > referenced from the global scope when I want it to be local. I all
        > actuality, I'm not even sure how it's calling from the global scope in
        > this case.
        >
        > Here is my situation, in code:
        >
        > var myObj = function() {
        > return {
        >
        > vars:null,
        > init: function() {
        >
        > //do stuff
        > this.myACDataSource = new YAHOO.widget.DS_JSFunction(this.someFunc);
        >
        > }
        >
        > someFunc: function() {
        > this.myvar = 'foo';
        > }
        >
        > }
        > }
        >
        > So, when I debug this w/Firebug with a breakpoint on the line
        > this.myvar = 'foo', firebug tells me:
        >
        > this = window test.jsp
        >
        > I'm so very confused as to how this is the case when clearly the
        > function called was in the scope of myObj. I've worked around it by
        > calling this.myObjInstance.myvar, but I feel that this is a hack and
        > there should be a better way to do this.
        >
      • jeremymefford
        So that worked perfect to fix my scope issue, however now my param from the autocomplete is not getting passed into the function...
        Message 3 of 5 , Apr 1, 2008
          So that worked perfect to fix my scope issue, however now my param
          from the autocomplete is not getting passed into the function...

          --- In ydn-javascript@yahoogroups.com, "y_lsmith" <lsmith@...> wrote:
          >
          > Jeremy,
          >
          > The JavaScript interpreter dereferences this.someFunc to the function
          > itself (not the function scoped to the this object) before passing it to
          > the DS_JSFunction constructor. Functions do not carry their scope with
          > them; this is determined when they are executing.
          >
          > For example:
          >
          > var someFunc = function () {
          > alert(this.name); // who is this?
          > };
          >
          > var MyClass = function (name) {
          > this.name = name;
          > this.fooFunc = someFunc;
          > };
          >
          > var MyOtherClass = function (name) {
          > this.name = name;
          > };
          > MyOtherClass.prototype.barFunc = someFunc;
          >
          > // this is the window object
          > someFunc();
          >
          > // this is the MyClass instance
          > new MyClass('foo').fooFunc();
          >
          > // this is the MyOtherClass instance
          > new MyOtherClass('bar').barFunc();
          >
          > var baz = { name: 'baz' };
          > // this is baz
          > someFunc.call(baz);
          >
          > You can avoid the this confusion by creating a closure around your
          > instance method like so:
          >
          > init : function () {
          > //do stuff
          >
          > var me = this;
          > this.myACDataSource = new YAHOO.widget.DS_JSFunction(function () {
          > me.someFunc();
          > });
          > },
          >
          > Hope this helps,
          > Luke
          >
          > --- In ydn-javascript@yahoogroups.com, "jeremymefford"
          > <jeremymefford@> wrote:
          > >
          > > I'm not quite sure how to fix this problem I'm having. I created a
          > > custom object that contains an autocomplete widget with a datatable.
          > >
          > > I used the example off the YUI site that had me use DS_JSFunction for
          > > my autocomplete widget, however the oFunction that it's calling is
          > > referenced from the global scope when I want it to be local. I all
          > > actuality, I'm not even sure how it's calling from the global scope in
          > > this case.
          > >
          > > Here is my situation, in code:
          > >
          > > var myObj = function() {
          > > return {
          > >
          > > vars:null,
          > > init: function() {
          > >
          > > //do stuff
          > > this.myACDataSource = new
          > YAHOO.widget.DS_JSFunction(this.someFunc);
          > >
          > > }
          > >
          > > someFunc: function() {
          > > this.myvar = 'foo';
          > > }
          > >
          > > }
          > > }
          > >
          > > So, when I debug this w/Firebug with a breakpoint on the line
          > > this.myvar = 'foo', firebug tells me:
          > >
          > > this = window test.jsp
          > >
          > > I'm so very confused as to how this is the case when clearly the
          > > function called was in the scope of myObj. I've worked around it by
          > > calling this.myObjInstance.myvar, but I feel that this is a hack and
          > > there should be a better way to do this.
          > >
          >
        • y_lsmith
          Maybe you need to include the pass through arguments in the anonymous function wrapper? var me = this; this.myACDataSource = new
          Message 4 of 5 , Apr 1, 2008
            Maybe you need to include the pass through arguments in the anonymous function wrapper?

            var me = this;
            this.myACDataSource = new YAHOO.widget.DS_JSFunction(function (a,b,c) {
                me.someFunc(a,b,c);
            });

               
            Or a more broad brush approach:
            var me = this;
            this.myACDataSource = new YAHOO.widget.DS_JSFunction(function () {
                someFunc.apply(me, arguments);
            });


            Hope this helps,
            Luke

            --- In ydn-javascript@yahoogroups.com, "jeremymefford" <jeremymefford@...> wrote:
            >
            > So that worked perfect to fix my scope issue, however now my param
            > from the autocomplete is not getting passed into the function...
            >
            > --- In ydn-javascript@yahoogroups.com, "y_lsmith" lsmith@ wrote:
            > >
            > > Jeremy,
            > >
            > > The JavaScript interpreter dereferences this.someFunc to the function
            > > itself (not the function scoped to the this object) before passing it to
            > > the DS_JSFunction constructor. Functions do not carry their scope with
            > > them; this is determined when they are executing.
            > >
            > > For example:
            > >
            > > var someFunc = function () {
            > > alert(this.name); // who is this?
            > > };
            > >
            > > var MyClass = function (name) {
            > > this.name = name;
            > > this.fooFunc = someFunc;
            > > };
            > >
            > > var MyOtherClass = function (name) {
            > > this.name = name;
            > > };
            > > MyOtherClass.prototype.barFunc = someFunc;
            > >
            > > // this is the window object
            > > someFunc();
            > >
            > > // this is the MyClass instance
            > > new MyClass('foo').fooFunc();
            > >
            > > // this is the MyOtherClass instance
            > > new MyOtherClass('bar').barFunc();
            > >
            > > var baz = { name: 'baz' };
            > > // this is baz
            > > someFunc.call(baz);
            > >
            > > You can avoid the this confusion by creating a closure around your
            > > instance method like so:
            > >
            > > init : function () {
            > > //do stuff
            > >
            > > var me = this;
            > > this.myACDataSource = new YAHOO.widget.DS_JSFunction(function () {
            > > me.someFunc();
            > > });
            > > },
            > >
            > > Hope this helps,
            > > Luke
            > >
            > > --- In ydn-javascript@yahoogroups.com, "jeremymefford"
            > > <jeremymefford@> wrote:
            > > >
            > > > I'm not quite sure how to fix this problem I'm having. I created a
            > > > custom object that contains an autocomplete widget with a datatable.
            > > >
            > > > I used the example off the YUI site that had me use DS_JSFunction for
            > > > my autocomplete widget, however the oFunction that it's calling is
            > > > referenced from the global scope when I want it to be local. I all
            > > > actuality, I'm not even sure how it's calling from the global scope in
            > > > this case.
            > > >
            > > > Here is my situation, in code:
            > > >
            > > > var myObj = function() {
            > > > return {
            > > >
            > > > vars:null,
            > > > init: function() {
            > > >
            > > > //do stuff
            > > > this.myACDataSource = new
            > > YAHOO.widget.DS_JSFunction(this.someFunc);
            > > >
            > > > }
            > > >
            > > > someFunc: function() {
            > > > this.myvar = 'foo';
            > > > }
            > > >
            > > > }
            > > > }
            > > >
            > > > So, when I debug this w/Firebug with a breakpoint on the line
            > > > this.myvar = 'foo', firebug tells me:
            > > >
            > > > this = window test.jsp
            > > >
            > > > I'm so very confused as to how this is the case when clearly the
            > > > function called was in the scope of myObj. I've worked around it by
            > > > calling this.myObjInstance.myvar, but I feel that this is a hack and
            > > > there should be a better way to do this.
            > > >
            > >
            >
          • y_lsmith
            Maybe you need to include the pass through arguments in the anonymous function wrapper? var me = this; this.myACDataSource = new
            Message 5 of 5 , Apr 1, 2008
              Maybe you need to include the pass through arguments in the anonymous function wrapper?

              var me = this;
              this.myACDataSource = new YAHOO.widget.DS_JSFunction(function (a,b,c) {
                  me.someFunc(a,b,c);
              });

                 
              Or a more broad brush approach:
              var me = this;
              this.myACDataSource = new YAHOO.widget.DS_JSFunction(function () {
                  someFunc.apply(me, arguments);
              });


              Hope this helps,
              Luke

              --- In ydn-javascript@yahoogroups.com, "jeremymefford" <jeremymefford@...> wrote:
              >
              > So that worked perfect to fix my scope issue, however now my param
              > from the autocomplete is not getting passed into the function...
              >
              > --- In ydn-javascript@yahoogroups.com, "y_lsmith" lsmith@ wrote:
              > >
              > > Jeremy,
              > >
              > > The JavaScript interpreter dereferences this.someFunc to the function
              > > itself (not the function scoped to the this object) before passing it to
              > > the DS_JSFunction constructor. Functions do not carry their scope with
              > > them; this is determined when they are executing.
              > >
              > > For example:
              > >
              > > var someFunc = function () {
              > > alert(this.name); // who is this?
              > > };
              > >
              > > var MyClass = function (name) {
              > > this.name = name;
              > > this.fooFunc = someFunc;
              > > };
              > >
              > > var MyOtherClass = function (name) {
              > > this.name = name;
              > > };
              > > MyOtherClass.prototype.barFunc = someFunc;
              > >
              > > // this is the window object
              > > someFunc();
              > >
              > > // this is the MyClass instance
              > > new MyClass('foo').fooFunc();
              > >
              > > // this is the MyOtherClass instance
              > > new MyOtherClass('bar').barFunc();
              > >
              > > var baz = { name: 'baz' };
              > > // this is baz
              > > someFunc.call(baz);
              > >
              > > You can avoid the this confusion by creating a closure around your
              > > instance method like so:
              > >
              > > init : function () {
              > > //do stuff
              > >
              > > var me = this;
              > > this.myACDataSource = new YAHOO.widget.DS_JSFunction(function () {
              > > me.someFunc();
              > > });
              > > },
              > >
              > > Hope this helps,
              > > Luke
              > >
              > > --- In ydn-javascript@yahoogroups.com, "jeremymefford"
              > > <jeremymefford@> wrote:
              > > >
              > > > I'm not quite sure how to fix this problem I'm having. I created a
              > > > custom object that contains an autocomplete widget with a datatable.
              > > >
              > > > I used the example off the YUI site that had me use DS_JSFunction for
              > > > my autocomplete widget, however the oFunction that it's calling is
              > > > referenced from the global scope when I want it to be local. I all
              > > > actuality, I'm not even sure how it's calling from the global scope in
              > > > this case.
              > > >
              > > > Here is my situation, in code:
              > > >
              > > > var myObj = function() {
              > > > return {
              > > >
              > > > vars:null,
              > > > init: function() {
              > > >
              > > > //do stuff
              > > > this.myACDataSource = new
              > > YAHOO.widget.DS_JSFunction(this.someFunc);
              > > >
              > > > }
              > > >
              > > > someFunc: function() {
              > > > this.myvar = 'foo';
              > > > }
              > > >
              > > > }
              > > > }
              > > >
              > > > So, when I debug this w/Firebug with a breakpoint on the line
              > > > this.myvar = 'foo', firebug tells me:
              > > >
              > > > this = window test.jsp
              > > >
              > > > I'm so very confused as to how this is the case when clearly the
              > > > function called was in the scope of myObj. I've worked around it by
              > > > calling this.myObjInstance.myvar, but I feel that this is a hack and
              > > > there should be a better way to do this.
              > > >
              > >
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.