473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Duplicate Definition in JavaScript.

2,476 2GB
Today i encountered a typical thing. So far what i knew that JavaScript had a scope inside a function but it was not there. I think in newer version of JavaScript it is introduced, in older version there may be. Anyway .....
Have a look at my code snippet ....

Expand|Select|Wrap|Line Numbers
  1. function test(){
  2. var s = "Hello!";
  3. var s = "Hi!";
  4. alert(s); //it alerts the latest value of s, and here is no duplicate definition of s
  5. }
Expand|Select|Wrap|Line Numbers
  1. function test(){
  2. //some code
  3. }
  5. function test(){
  6. //some latest code
  7. }
Whenever i call test it executes the latest code, here is also not duplicate definition. How JavaScript handles it?
Please explain.
Aug 7 '08 #1
3 1823
5,390 Expert Mod 4TB
with you first example you REdeclare var s within the function ... when you set the javascript-engine to handle the interpretation of the code in strict-mode in firefox/mozilla then the error console will show you that as a warning.

the second is OVERwriting ... basically functions are declared in the window-scope and there you overwrite the first with the second ... basically what you do is:

Expand|Select|Wrap|Line Numbers
  1. this.test = function() {
  2.     // some code 
  3. }
where this is the window-object's scope ...

kind regards
Aug 7 '08 #2
rnd me
427 Expert 256MB
scope is only preserved and layered in functions:
Expand|Select|Wrap|Line Numbers
  1. function test(){
  2.   function test(){
  3.     return test;
  4.   }
  5.  return test;
  6. }


test().toString() // =
Expand|Select|Wrap|Line Numbers
  1. function test() {
  2.     return test;
  3. }
test.toString() // =
Expand|Select|Wrap|Line Numbers
  1. function test() {
  3.     function test() {
  4.         return test;
  5.     }
  7.     return test;
  8. }
Aug 7 '08 #3
5,390 Expert Mod 4TB
ahh ... and to put FF into strict-mode ... type in the address-bar: about:config there a filter appears and you type in the filter: javascript.options.strict ... set its value to true -> see what a bunch of errors and information you now get in the console additionally :)

kind regards
Aug 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

by: cr | last post by:
Could somebody tell me whether the use of duplicate element definition or reference are permitted in XML schemas? For example: <complexType name="PurchaseOrderType"> <sequence> <element...
by: Randell D. | last post by:
Folks, I'm a newbie when it comes to JavaScript and whenever I visit a website curiosity sometimes get's the better of me and I'll inspect some of their code - It teaches me new things (either...
by: nick4soup | last post by:
I have read the CGI FAQ 'How can I avoid users hitting "submit" twice' (on http://www.htmlhelp.org/faq/cgifaq.3.html#19 ) which essentially says you have to detect it at the server, using a...
by: Asha | last post by:
greetings, does anyone have any idea how to delete duplicate rows in a dataset gracefully (the fast and easy way)
by: ucfcpegirl06 | last post by:
Hello, I have a dilemma. I am trying to flag duplicate messages received off of a com port. I have a software tool that is supposed to detect dup messages and flag and write the text "DUP" on...
by: Oleg Konovalov | last post by:
Hi, I have a Java/JavaScript GUI application where I perform a lot of long DB operations , which takes 5-60 secs to perform. Sometimes user double-clicks the button or just gets impatient and...
by: Dmitry Kulinich | last post by:
Guys! Is there are any possibility to create nodes with duplicate names and different types in XSD? I've read the whole specification and tried in a many different ways, but not successfull. ...
by: traceable1 | last post by:
I have a table with a unique non-clustered index on it. It has the IGNORE_DUP_KEY option on. For some reason, I am getting the following error: ...
by: mdh | last post by:
I decided to make a single file containing all the repetitive functions in K&R so that I could concentrate on the new discussions. This went along just fine, and with each new function, added the...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.