get name of var as a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arodicus
    New Member
    • May 2007
    • 12

    get name of var as a string?

    Given something like this:

    Code:
    var foo = new Something()
    is there any way for the Something class to know it's being put into "foo" (as a String)?

    The only thing I can come up with so far is blatantly feeding it:

    Code:
    var foo = new Something("foo")
    Which my client, of course, doesn't like doing... they need it as idiot-proof as possible.

    Background:

    At some point in the code, Something Class's references to "this" need to be converted to a string, exported to a plugin, and sent back to JavaScript... by which time, of course, "this" is no longer valid. To maintain state across this "must be a string" bridge, I'd like to know the name of the variable Something is first assigned to ( with the understanding that once instantiated it can be cross-assigned to other vars).

    Yeah, it's a weird request!
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    Something Class's references to "this" need to be converted to a string, exported to a plugin, and sent back to JavaScript
    You are not using the object model to its advantage- the object itself should have the methods, or be able to call the methods, you are trying to pass it to.

    Comment

    • Arodicus
      New Member
      • May 2007
      • 12

      #3
      No, that's not quite right... the methods in Something are more or less irrelevant, except that they need to pass "foo" (as a string) into the ActiveX/Plugin that it generates.

      In other words var "foo" is going to either be COMPLETELY REPLACED or at least needs to EVALUATE to something other than what it was originally created with.

      It must be a string, because the Plugin works by script injection - a JavaScript as an eval'd string is passed into the page. Thus:

      Code:
      var foo = new Something()
      
      function Something() {
          // do some stuff, and then
          // create an ActiveX/Plugin control
      }
      (ActiveX/Plugin does some stuff, then kicks out a string to be eval'd):
      Code:
      eval("var foo = new SomethingElse()")
      What I'm trying to do is find some kind of reference to the "foo" var that can be written as a string so that the ActiveX/Plugin (Flash in this case) will know what variable it needs to tweak.

      Actually, it doesn't even need to go that far, so long as a reference to the Object can be redirected to another object. I'm able to do that with Primitives like this:
      Code:
      function Something(){
          this.bar = "Foobar"
          val = 69;
          this.valueOf=this.toSource=this.toString=function(){return val}
      }
      
      var foo = new Something()
      
      alert(foo) // 69
      alert(foo.bar) // "Foobar"
      which is pretty cool, and even this kinda works, where val is an object instead of a Primitive:
      Code:
      function Something(){
          this.bar = "Foobar"
          val = widow;
          this.valueOf=this.toSource=this.toString=function(){return val}
      }
      
      var foo = new Something()
      
      alert(foo) // [object] (window)
      alert(foo.bar) // "Foobar" (Something's property "bar")
      alert(foo.document) // undefined (Something's nonexisting prop "document")
      alert(foo.valueOf().document) // [object] (window's prop "document")
      but attempting to access any properties of that Window object will fail, unless you explicitly say foo.valueOf().p roperty (which works). If I could get rid of that valueOf() statement, I'd be happy.

      AS2 had a cool little function called __Resolve that could be used to do this, so if you were looking at Class object and called a property or method on that object that wasn't there, it could be handed off to another function for proper evaluation... I wonder if JS has something like that?

      Comment

      Working...