User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 403,324 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,024 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 888 | Replies: 6
Reply
Join Date: Dec 2007
Location: Raleigh, NC
Posts: 31
Reputation: AaronASterling is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
AaronASterling's Avatar
AaronASterling AaronASterling is offline Offline
Light Poster

Scope issue with javascript.

  #1  
Apr 12th, 2008
Hello,

I am messing around with javascript. I am trying to create a function to which i can pass a constructor and recieve back an object. Kinda (read exactly) like the new keyword. call it a learning exercise. I have a working model but i don't like it. here it is

  1. function CREATE(constructor) {
  2. var o = new constructor();
  3. if (arguments.length > 1) {
  4. constructor.apply(o, Array.prototype.slice.apply(arguments, [1]));
  5. }
  6. return o;
  7. }

I don't like it because it uses the new keyword on the constructor and then calls the constructor again if there are any arguments to be passed on it. kinda stupid. More importantly its not what i first wrote and since i don't understand why what i first wrote doesn't work, im agrivated. What I want is
  1. function CREATE(constructor) {
  2. var o = new Object();
  3. o.prototype = constructor.prototype;
  4. if (arguments.length > 1) {
  5. constructor.apply(o, Array.prototype.slice.apply(arguments, [1]));
  6. }
  7. return o;
  8. }

here is some test code to illustrate how this breaks....

    function TestConstructor(value) {
        this.value = this.dummy(value);
    }

    TestConstructor.prototype.dummy = function (value) {
        return value;
    };
    
    var testObject = CREATE(TestConstructor, 1); 

javascript says that dummy does not exists. I run it in firebug and just before the this.value = this.dummy(value); line is run, this is an object that has a prototype member and that prototype member has a dummy method. From what i understand, javascript should try to find dummy as a method of this, fail, then look for it as a member of this.prototype and succeed. I can see that it is there in firebug. Does anyone have any idea what is going on?

Thanks,
Aaron.
Aaron Sterling
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Location: Raleigh, NC
Posts: 31
Reputation: AaronASterling is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
AaronASterling's Avatar
AaronASterling AaronASterling is offline Offline
Light Poster

Re: Scope issue with javascript.

  #2  
Apr 12th, 2008
Ok...when i change the line this.value = this.dummy(value) to this.value = this.prototype.dummy(value) the code runs. Does anybody have any idea what this is about? isn't the point of the prototype member that methods and members contained in it can be accessed indirectly as I was originally trying to do? there is deep understanding awaiting anyone that wants to come and play with me.
Aaron Sterling
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Scope issue with javascript.

  #3  
Apr 12th, 2008
> Ok...when i change the line this.value = this.dummy(value) to this.value =
> this.prototype.dummy(value) the code runs.

Are you sure this works, because IMO it shouldn't. Try to instantiate your object in a normal manner and see this line blow up. The prototype property of the object is not the same as the prototype property of the Constructor object. Each object created using a constructor has an *implicit* reference to it's Constructors' prototype. It works in your case because in your CREATE function you end up assigning the prototype property of the Constructor object (TestConstructor) to the prototype property of the newly created object.

The only way to make your dummy() function not blow up is to call it as TestConstrutror.prototype.dummy() and not this.prototype.dummy() for the reasons mentioned above.

As far as your issue is concerned, I think that as long as an object isn't created, the context for property lookup using the prototype chain is not set up. So when the call to dummy function inside the constructor is made, a implicit link has not been set up between the object which is *yet to be created* and the prototype property of the Constructor. This can be verified by the fact that once the Constructor exits and a new object is returned, the call to dummy() works as it should. E.g.
TestConstructor.prototype.dummy = function (value) {
  return value * 2;
};
    
function TestConstructor(value) {
  this.value = this.prototype.dummy(value);  // won't work
};

var a = new TestConstructor(1);
a.dummy(); // This works!

Just my 2 cents...
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jan 2007
Posts: 2,537
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 111
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: Scope issue with javascript.

  #4  
Apr 14th, 2008
The thing that you don't see is that the object is created globally. The thing passed around in the function calls is a pointer to the object, not the object itself.
Last edited by MidiMagic : Apr 14th, 2008 at 4:01 pm.
Daylight-saving time uses more gasoline
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Scope issue with javascript.

  #5  
Apr 14th, 2008
> The thing that you don't see is that the object is created globally.

What? Which object? And whatever happened to scoping rules in Javascript?

> The thing passed around in the function calls is a pointer to the object, not the object itself.

You are way off the actual topic under discussion.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jan 2007
Posts: 2,537
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 111
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: Scope issue with javascript.

  #6  
Apr 16th, 2008
The scoping rules are slightly different for objects.

With a variable, the scoping is as you would expect it. But with an object (an array or some other kind of construct), the scoping is quite different.

When you pass an object, or part of an object as a function parameter, you are actually sending a pointer that tells where the object is. This usually works as expected, but the function can not return a changed object back through a parameter.
Last edited by MidiMagic : Apr 16th, 2008 at 9:02 pm.
Daylight-saving time uses more gasoline
Reply With Quote  
Join Date: Dec 2007
Location: Raleigh, NC
Posts: 31
Reputation: AaronASterling is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
AaronASterling's Avatar
AaronASterling AaronASterling is offline Offline
Light Poster

Re: Scope issue with javascript.

  #7  
Apr 16th, 2008
Originally Posted by MidiMagic View Post
When you pass an object, or part of an object as a function parameter, you are actually sending a pointer that tells where the object is. This usually works as expected, but the function can not return a changed object back through a parameter.



  1. function modifyObject(o) {
  2. o.value = 'Bullocks to that';
  3. return o;
  4. }
  5.  
  6. obj = new Object();
  7. alert((modifyObject(obj)).value);
Last edited by AaronASterling : Apr 16th, 2008 at 9:33 pm. Reason: forgot a closing paren in the code
Aaron Sterling
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 7:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC