PDA

View Full Version : Forward Referencing & Forward Declaration



cloud_sync
Mar 4th, 2011, 05:51 PM
What are these used for? How are they done in JS? Any refs online?

Old Pedant
Mar 5th, 2011, 12:45 AM
See my other posts.

In JavaScript, you never need a forward declaration (unlike C and C++) and forward references are automatic.

NOTE: don't confuse this with uninitialized and unassigned references!

That is

var a = b;
var b = 17;

will *NOT* assign 17 to a! That's because assignments are of course done in program order.

cloud_sync
Mar 6th, 2011, 08:18 PM
Alright. What is a "forward reference" then? Does the "forward" have any significance?

Philip M
Mar 7th, 2011, 07:43 AM
Are we talking about regular expressions?

Some langauges including Java and Perl, allow forward references. That is: you can use a backreference to a group that appears later in the regex. This is not valid in Javascript.

Or do you mean function declarations?

http://stackoverflow.com/questions/2072685/ambiguous-function-declaration-in-javascript

Frustration arises the first time you cannot do it the second time. Desperation sets in the second time you cannot do it the first time.

Old Pedant
Mar 7th, 2011, 07:56 PM
Philip: See his other posts. He's used to C/C++ where you can't make a forward reference to a function that hasn't yet been defined.

Reasonably thoroughly covered in the other threads he started.

cloud_sync
Mar 7th, 2011, 11:32 PM
Are we talking about regular expressions?

Some langauges including Java and Perl, allow forward references. That is: you can use a backreference to a group that appears later in the regex. This is not valid in Javascript.

Or do you mean function declarations?

http://stackoverflow.com/questions/2072685/ambiguous-function-declaration-in-javascript

Frustration arises the first time you cannot do it the second time. Desperation sets in the second time you cannot do it the first time.

Oh, that clears the air. The C++ statement was regarding the awareness of the equality operator types. I don't understand why Old Pedant was mentioning "forward references" in the first place. Maybe he was pertaining to pointers.

Old Pedant
Mar 7th, 2011, 11:39 PM
*sigh* you were give a nice answer to this in another thread. JavaScript does what is called "hoisting" which has the same effect--in most cases--as allowing forward references.

Anyway, you were the person who used the term "forward referencing" as part of the title of this thread.

rnd me
Mar 8th, 2011, 12:19 AM
you can use a getter to create a virtual forward reference.

there are formal ways of defining true getters in all modern browsers.

.valueOf() and .toString() are built in customizable getters that work in all browsers.
if you overload .valueOf(), don't forget to coerce the results into the type you expect.

an example:


var forwardX={
valueOf:function(){return x;},
toString:function(){return x+'';}
};//end forwardX

x="this is now x";

alert(forwardX);

while you can do the same thing as above with just closures, you can do more stuff in your valueOf methods, like checking x for undefined- providing a default if invalid.

note that primitives are passed and assigned by value, so you can't affect the forwarded value, unless you use a custom setter, which is not compatible with old browsers.
you can upgrade primitives to a one-element array, which allows object reference, but then you have to add "[0]" after all the refs to the old primitive in your code. can be worth it.

cloud_sync
Mar 8th, 2011, 12:28 AM
.




Anyway, you were the person who used the term "forward referencing" as part of the title of this thread.

Quite frankly, that's how much you intrigued me about them in the other thread. I couldn't find any JS tutorials regarding them. I'm a learner not a peer.



@ rnd me: Virtual forward reference. Hmm. I'll have to review this.



.