| | |
Scope issue with javascript.
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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
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
here is some test code to illustrate how this breaks....
javascript says that dummy does not exists. I run it in firebug and just before the
Thanks,
Aaron.
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
javascript Syntax (Toggle Plain Text)
function CREATE(constructor) { var o = new constructor(); if (arguments.length > 1) { constructor.apply(o, Array.prototype.slice.apply(arguments, [1])); } return o; }
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
javascript Syntax (Toggle Plain Text)
function CREATE(constructor) { var o = new Object(); o.prototype = constructor.prototype; if (arguments.length > 1) { constructor.apply(o, Array.prototype.slice.apply(arguments, [1])); } return o; }
here is some test code to illustrate how this breaks....
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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
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
> 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
The only way to make your
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
Just my 2 cents...
> 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. JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
> 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.
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.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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.
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 10:02 pm.
Daylight-saving time uses more gasoline
•
•
•
•
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.
javascript Syntax (Toggle Plain Text)
function modifyObject(o) { o.value = 'Bullocks to that'; return o; } obj = new Object(); alert((modifyObject(obj)).value);
Last edited by AaronASterling; Apr 16th, 2008 at 10:33 pm. Reason: forgot a closing paren in the code
Aaron Sterling
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Any pointer on how the developer made this
- Next Thread: setInterval inside function issue
Views: 1720 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxcode ajaxhelp animate array automatically autoplay beta boarder box bug button calendar captcha card cart codes column cookies createrange() css cursor date debugger decimal design developer dom download dropdown element enter error events firefox firehose flash focus form frameworks getselection google gwt html htmlform iframe image() images index java javascript javascripts jawascriptruntimeerror jquery jsp listbox maps marquee masterpage menu microsoft mimic mp3 mp4 offline onmouseover parameters php player post problem programming prototype rating redirect regex safari scale scriptlets search select size sources sql starrating text textarea toggle twitter validation variables w3c web website window windowofwords windowsxp xml xspf






