Results 1 to 3 of 3

Thread: Adding to non-global, non-current scope from current scope?

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Adding to non-global, non-current scope from current scope?

    Is there any way to add to a non-global, non-current scope in Javascript from the current scope?

    I want to do something to the effects of:
    Code:
    function a() {
    	b();
    	c();
    	}
    function b() {
    	b.caller.c = function() {
    		alert("c made from b");
    		};
    	}
    a();
    This doesn't work. Is what I'm trying to accomplish strictly impossible? (Feasibility is not an issue.)

    I'm trying to make an Import script that imports code into the scope that called Import(). If I defined Import to return a string, I could do eval(Import("script.js")), but I want to make it so you only have to manually call one function.
    Last edited by Trinithis; 09-17-2007 at 09:35 PM.
    Trinithis

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    I believe you could use

    Code:
    function a() {
    	this.call( b() );
    	this.call( c() );
    	}
    function b() {
    	b.caller.c = function() {
    		alert("c made from b");
    		};
    	}
    a();
    but I may be wrong

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yup, you're wrong on two counts:
    Code:
            this.call( b() );
    You want to pass the function b itself to call(), not the return value of the function, so those brackets shouldn't be there. Then, this in that scope refers to the global object. I think you've misunderstood its purpose: it refers to the object as a property of which the function was called, not the function itself. E.G.:
    Code:
    var a = {
      b: {
        c: function() {
          // here, this refers to a.b, not a.b.c.
        }
      }
    };
    This doesn't work.
    No. Adding properties to a function object doesn't create them as variables within the function's scope.
    Is what I'm trying to accomplish strictly impossible?
    As far as I'm aware.
    If I defined Import to return a string, I could do eval(Import("script.js")), but I want to make it so you only have to manually call one function.
    That's ugly. In fact, the original Import() idea was ugly too. The neat solution is to have import() (without a capital, since that's reserved for constructors) return an object, which acts both as a convenient container for the multiple return values and a namespace for the calling function.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •