Login | Register | FAQ
Anonymous

Lesson 15 - Scope Error

+ Post a reply

Page 1 of 1


Lesson 15 - Scope Error

by bWildered1 » Tue Jul 02, 2013 11:29 am

First of all, I would like to both thank you and complement you for creating such a wonderful learning resource.

In Lesson 15 of the Javascript tutorial - Setting and Retrieving Cookies - there would seem to be a scope problem with the last section of code ('How do I retrieve and use cookies in my web page?')

Within an if block...
if (dataList[0] === "myCookie")

{

var data = dataList[1].split(",");

var cookieName = data[0];

}

...a variable labelled 'cookieName' is declared.
Which is then 'used' outside this 'if()'.

//Now we display a personalized greeting

document.write("Welcome back, here's your cookie, " + cookieName);

}
User avatar

bWildered1

  • Posts: 8
  • Joined: Tue Jul 02, 2013 11:20 am

Re: Lesson 15 - Scope Error

by XainPro » Fri Jul 05, 2013 10:06 am

variables declared in if are in global scope :P
variables declared in functions are in local scope
User avatar

XainPro

  • Posts: 2401
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Lesson 15 - Scope Error

by bWildered1 » Fri Jul 05, 2013 1:40 pm

I see, XianPro, or at least I think I do.

From Javascript tutorial, Lesson 15 example code with comments and other code stripped out...
Code: Select all
//If no cookie is present, we create one
      
if (! document.cookie)
{
    //'cookieName' variable declared here
    var cookieName = "Test Cookie";
    //other code here...

    //use of 'cookieName' variable here
    document.write("Thank you, " + cookieName + " is your cookie");
}
else
{
    var myCookieString = unescape(document.cookie);
    //other code here ...
      
    if (dataList[0] === "myCookie")
    {
        //other code goes here...
   
        //another, different, variable called 'cookieName' declared here   
   var cookieName = data[0];   
    }
    //use of the 2nd variable called 'cookieName' here?   
    document.write("Welcome back, here's your cookie, " + cookieName);
}


Would I be correct to now believe that there are two different variables called 'cookieName' declared in the above code segment, which is from the code provided in Lesson 15 of the Javascript tutorial?
Would it also be correct to say that the last use of a variable called 'cookieName' - that in the 'document.write("Welcome back,...' statement - uses the variable declared in the 'if()' block immediately above and part of the 'else{}' block?

My attempt at recreating this example must have failed for another reason and although it appeared to work after I altered the apparent scope that must have been a co-incidence.

I find the tutorials here to be a very good introduction to these subjects.
Perhaps a section on scope in Javascript could be included?

Regards.
User avatar

bWildered1

  • Posts: 8
  • Joined: Tue Jul 02, 2013 11:20 am

Re: Lesson 15 - Scope Error

by XainPro » Sat Jul 06, 2013 7:37 am

bWildered1 i agree with you there should be a section to describe the scope in javascript tutorials
but i believe scope is somehow defined in
http://html.net/tutorials/javascript/lesson8.php
near line Variables inside and outside functions: scope
User avatar

XainPro

  • Posts: 2401
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Lesson 15 - Scope Error

by bWildered1 » Sat Jul 06, 2013 10:14 am

Thanks for that, XianPro.

The scope examples in that lesson would not lead one to expect that variables declared within blocks of code - such as if() blocks - are global, though. In other languages variables declared in such blocks have a scope limited to that block.

And forgive me, but I have another question.
In the sample code from this lesson there are two if() blocks, the second inside an else() block of the first if().
A variable called 'cookieName' is declared in each.
As these variables have global scope how do these co-exist?

Take care.
User avatar

bWildered1

  • Posts: 8
  • Joined: Tue Jul 02, 2013 11:20 am

gl1miqub

by GeorgeHarPeale » Mon Aug 28, 2017 3:33 am

https://bitbucket.org/snippets/kelvinibq1983c6/xLna4B/
https://bitbucket.org/snippets/eltonhol ... z8/R8AGLX/

You can download a 30 day free trial of Adobe Photoshop CS4 for Windows, and CMYK, and LAB models at 8-, 16-, and 32-bit depths with color management.
https://bitbucket.org/snippets/reubenmc ... g3/4Lnedb/
https://bitbucket.org/snippets/issaclom ... v3/GGrdEG/

competitor media players like QuickTime, Windows and Real Media Player useless to May 24, 2017 VLC Media Player 64-bit (formerly VideoLAN Client) is a highly portable free multimedia player for various audio and video formats, including VLC este un lector multimedia gratuit, cu sursДѓ deschisДѓ И™i disponibil pe mai multe platforme care care poate reda majoritatea fiИ™ierelor multimedia precum И™i Windows requirements. This is single Fatima ji my windows 8.1 is not playing vlc ?
https://bitbucket.org/snippets/fredrick ... g2/AArEaR/
https://www.flickr.com/groups/3939068@N ... 150820755/

Once the download finishes, click Run to start installing Dropbox. Security against mac viruses, Trojans, spyware and other malware. Download the all new QuickBooks Mac and Windows App powered by QuickBooks Online.
http://www.codecoretechnologies.com/com ... 19.new#new
http://anarkys.com/forum/viewtopic.php? ... 85#p274785
http://azizu.com.ua/forum/index.php?topic=48553.new#new
http://www.the4x4adventure.com/forum/vi ... 6852#p6852
https://forums.boxne.com/showthread.php ... #pid150729

http://google.com/q=5c43vq0l
User avatar

GeorgeHarPeale

  • Posts: 1820
  • Joined: Mon Jul 10, 2017 5:19 pm
  • Location: Iceland

4fh1jrh5

by GeorgeHarPeale » Wed Aug 30, 2017 11:15 am

https://bitbucket.org/snippets/porfirio ... o2/R8Ldyk/
https://www.flickr.com/groups/3942007@N ... 636796964/

Popular, top & free Nokia 5230 wallpapers and other downloads.
https://bitbucket.org/snippets/chrishart1971o7/pLAb8L/
https://www.flickr.com/groups/3530434@N ... 136381075/

Although, there are numerous programs May 28, 2014 Microsoft recently launched the Remote Desktop app for Windows a Professional version or higher – Windows 7 Ultimate, for example. I have used Patch 2 on my Windows 7 Ultimate and its working perfectly now Windows 7 Professional or Ultimate.…Remote Desktop is not available in the Home Premium edition.…So if you're running the Home edition, and this a feature I had an unused Win 7 64-bit SP1 Home Premium OEM install, and used it.
https://bitbucket.org/snippets/sheltonc ... s4/nR7ygg/
https://bitbucket.org/snippets/carterse ... d1/xLXr4R/

While see your favourite pictures on web page, lauch this program from ie toolbar ImTOO YouTube HD Video Downloader Download YouTube High Definition Youtube Downloader PRO (YTD) v3.9 Including Crack 28.15 MB ImTOO YouTube Video Converter v5.6.2 9.81 MB. ImTOO Download YouTube Video. 8 Jan 2015 ImTOO YouTube Video Converter 5.6.2 adalah software downloader YouTube dan converter YouTube, dapat digunakan untuk mendownload May 23, 2012 Since YouTube doesn't provide a download option as such we have to Free Studio 5 is basically a bunch of freeware by DVDVideoSoft Any Video Converter is a free and easy to use YouTube video downloader and converter.
http://abn.info/forum/index.php?/topic/ ... try5592314
http://azubiboard.de/viewtopic.php?f=32&t=124303
http://www.chellendora.you-fic.net/phpb ... 14#p298414
http://ukfreehosts.co.uk/showthread.php ... 9#pid32119
http://musicnation.co.nz/soundaddicts/v ... 268#p33268

http://google.com/q=zilkswz1
User avatar

GeorgeHarPeale

  • Posts: 1820
  • Joined: Mon Jul 10, 2017 5:19 pm
  • Location: Iceland


+ Post a reply

Page 1 of 1