JavaScript is not C#

1,693 views

Published on

You can’t swing a stick in the ASP.NET world without hitting some JavaScript. And even though both languages have curly braces, JavaScript is not C#. This presentation will cover common misconceptions about JavaScript, some hidden gotchas, and areas of danger for C# developers that expect JavaScript to work like C#.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,693
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Introduction of selfIntroduction of topicImportance of JavaScriptImportance of knowing it well
  • What type is the “something” variableBlack and white
  • The similarities …
  • And the differences ….These are the falsey valuesDEMO – run once with test undefinedWhat about the word “False”, or the word “false”?
  • This is where the definition of falsey comes inIf false is like false, and 0 is like false, and you compare them …String / Number comparison can be deadly
  • Why can’t C# do it? Types
  • Making JS check the types as wellAlso mention !==
  • Trivia question: where does the movie variable live? How about anotherMovie? DEMO
  • DEMO
  • Talk more about function level scope laterWhere does movie live? Private variable of the function
  • Because we didn’t tell JS to declare new, it assumes referring to object on the window. Warning from Resharper 6Note that movie won’t be on the WINDOW until the function is calledBonus question – where does the function live?
  • DEMO
  • Local, so long as declared in the functionDEMO – prove that movie is local and sequel is global by moving console.log() outside of the block
  • Why is “efficiency” red?
  • DEMORewrite code to declare in the right spot
  • Rewrite the demo
  • No classDynamic nature
  • Object literal
  • DEMO creating objects
  • Talk about namespaces in C# and XML
  • DEMO
  • DEMO – show that now only stldodn is in the global namespaceDEMO – show that message is still in there
  • DEMO – show that message is not in there any more
  • DEMO creating objects
  • JavaScript is not C#

    1. 1. JavaScript is not C#<br />Brad Tutterow<br />www.codebadger.com<br />Swank Motion Pictures<br />
    2. 2.
    3. 3. Falsey values<br />
    4. 4. Checking for false (C#)<br />
    5. 5. Checking for false (JS)<br />
    6. 6. Checking for false (JS)<br />
    7. 7. Checking for null or empty (C#)<br />
    8. 8. Checking for null or empty (JS)<br />
    9. 9. Tip: Check for null by asking if the object is false<br />
    10. 10. Default values (C#)<br />
    11. 11. Default values (JS)<br />
    12. 12. Comparingfalsey values (JS)<br />
    13. 13. Comparing falsey values (C#)<br />
    14. 14. Comparing falsey values (JS)<br />
    15. 15. Tip: Use triple-equal to be clear that you really want things to be equal<br />
    16. 16. The global namespace<br />
    17. 17. The global namespace (JS)<br />
    18. 18. Tip: Top-level variables live on the window object in the DOM<br />
    19. 19. The global namespace (JS)<br />
    20. 20. The global namespace (JS)<br />
    21. 21. The global namespace (JS)<br />
    22. 22. The global namespace (JS)<br />
    23. 23. Tip: Always declare variables with the “var” keyword<br />
    24. 24. Function scope<br />
    25. 25. Function scope (JS)<br />
    26. 26. Tip: Always declare variables with the “var” keyword<br />
    27. 27. Block scope (C#)<br />
    28. 28. Block scope (JS)<br />
    29. 29. Tip: Declare all of a function’s variables at the top of the function<br />
    30. 30. Objects<br />
    31. 31. Creating a new object (C#)<br />
    32. 32. Creating a new object (JS)<br />
    33. 33. Creating a new object (JS)<br />
    34. 34. Adding a method (C#)<br />
    35. 35. Adding a method (JS)<br />
    36. 36. Tip: JavaScript methods are properties that are assigned a function <br />
    37. 37. Namespacing<br />
    38. 38. Cluttering up the global namespace (JS)<br />
    39. 39. Using an object literal (JS)<br />
    40. 40. Using jQuery ready (JS)<br />
    41. 41. Tip: Avoid cluttering the global namespace<br />
    42. 42. Tip: Check for null by asking if the object is false<br />
    43. 43. Tip: Use triple-equal to be clear that you really want things to be equal<br />
    44. 44. Tip: Top-level variables live on the window object in the DOM<br />
    45. 45. Tip: Always declare variables with the “var” keyword<br />
    46. 46. Tip: Declare all of a function’s variables at the top of the function<br />
    47. 47. Tip: JavaScript methods are properties that are assigned a function <br />
    48. 48. Tip: Avoid cluttering the global namespace<br />

    ×