1

Not sure I can word this right, so here's some code:

Note: this is in a file called a.js which is a dojo module

define([], function() {
var j = {
    a : function() {
        //a object
    },
    b : function() {
        //b object
        this.x = new j.a();// this works
        this.y = new this.a(); // this causes error later, see jsfiddle link below
    },
    c : function() {
        //c object
    }
}
return j;
});

I simply want object b when creating a new 'a' object to do it as new (b's parent).a(); rather than new j.a(); where variable j is happens to be b's parent. Not sure if that explanation helped, but I basically want a relative way of creating a new 'a' object from b.

Error is reproduced in this code: http://jsfiddle.net/WhhV5/3/

Not sure how to run dojo stuff in jsfiddle so the code may not run, but these are the two files used on my home xampp

Derek
  • 11,980
  • 26
  • 103
  • 162
  • Do you have control over the definition of the object? Also, instead of providing examples, can you get to the point, and show your **real** problem? – Rob W Feb 18 '12 at 14:41
  • when I try to do this in b: this.x = new this.a();, I get Uncaught TypeError: undefined is not a function in chrome. Does it matter that this is declared in a dojo module? – Derek Feb 18 '12 at 14:43
  • I not sure what you mean by show your real problem, my module is 1000+ lines of code, and this is the only line that has a problem, so I think providing an example is best. – Derek Feb 18 '12 at 14:44
  • @Derek - I think a better example to demonstrate this would be http://jsfiddle.net/WhhV5/. You're using `j` as like a namespace, and `a, b, c` being "classes" under the namespace. – DashK Feb 18 '12 at 14:45
  • @Derek Post the full relevant code + real question at http://pastebin.com/ Make sure that you clearly show what problem you're facing. Your current question is quite vague, and it's unlikely that you get the right answer without providing more details. – Rob W Feb 18 '12 at 14:46
  • @Rob W, I am facing a real problem, I'm using Dojo1.7.1 and have my own module that has objects. In one of the objects, like the example above, it refers to another object in the same module. When I initiate that object, it gives me an error if in b I have this.bMember = new this.a();, but it works if I have this.bMember = new j.a(); – Derek Feb 18 '12 at 14:49
  • @Derek I can't read your code at this distance. I'm sure that your intentions are good, but without any code, we cannot find the cause of your problem. – Rob W Feb 18 '12 at 14:52
  • @Derek - I think you might want to understand what the `new` keyword does. See http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript Once you create an instance of `b` using the `new` keyword, `b` no longer has access to members of `j` - `b` has `j` in its scope, but `this` in `b` is no longer `j` - It's `b` itself. You will have to reference `j` to get to `a` from `b` at that point. (Deleting my answer since it's no longer relevant to your question.) – DashK Feb 18 '12 at 14:53
  • I've reproduced the same error in this code: http://jsfiddle.net/WhhV5/2/ – Derek Feb 18 '12 at 14:54

1 Answers1

0

Frankly, here's where you don't make sense:

In other words, I want to call j.a(), j.c() in j.b() such that if I change the var j's name to var m, I won't have to change the function calls to m.a(), m.c().

You cannot change a variable's name in JavaScript. Variables are references of Objects not the Objects themselves (i.e. the range of memory allocated to a data structure).

If you do reference the value referenced by j with another variable m, j will still exist in memory. You simply created another reference to the Object which j references as well. You did not change its name.

So, if you call m#b it will call j#a and j#c without a problem as j still exists.

var j = {
    a: function( ) { console.log("(!!) a") ; } , 
    b: function( ) { new this.a( ) ; } , 
    c: function( ) { console.log("(!!) b") ; } , 
} ; 

var m = j ;
    m.b( ) ;
FK82
  • 4,907
  • 4
  • 29
  • 42
  • sorry if my explanation was really bad, i edited my question a bit, though i'm not sure it's any clearer. But what I really want is not a quick shortcut fix, but a conceptual relative way of creating/calling a from b. – Derek Feb 18 '12 at 15:16
  • I understand that you want an abstract approach here. What I don't think you realize is that the problem does not exist. Here try the above code on [JSBin](http://jsbin.com/ovohok/edit#javascript,html). It works without a problem. – FK82 Feb 18 '12 at 15:24
  • Can't help you with that, I'm afraid. Seems like it would however be a good idea to rethink your coding strategy for writing this module. Good luck! – FK82 Feb 19 '12 at 15:26