Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Fundamentals of JavaScript

Do you know JavaScript? Or do you just use JavaScript as if it were another C-like language?
by

Michael Wheeler

on 25 February 2011

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Fundamentals of JavaScript

Do you Know JavaScript? Or Just Use Javascript? If you don't know your tool Poor productivity Stress Bugs Confusion Maintenance Issues Let's do Something About It.
Right Now. Bad JavaScript is bad for your health We'll walk through a short quiz of JavaScript Related questions Poor Efficiency Missed Opportunities Poor Performance The plan Answer the questions to the best of your knowledge When you click "Next" We'll Zoom in on the Answer If the Result Surprises you, Read through the Explanation. Or else just move on to the next question <script>
function test() {
inner = "inside";
}
test();
answer = inner;
</script> a) "inside"
c) undefined
d) false
e) null Why? In JavaScript, variables should always be declared with the var keyword. // look for d in previous scope(s)
var a = 7; // look for d as an argument
function example(b) { // look for d in this scope
var c = 6; // d is declared without var
d = "new value"; } answer is a The var keyword was never used in this script Always remember to use the var keyword
to save yourself a headache Don't forget for loops //globals i, ii created
for(i=0, ii=arr.length; ...

//non-global
for(var i=0, ii=arr.length;... “[JavaScript is] the only language that I’m aware of that people feel they don’t need to learn it… before they start using it.”
– Douglas Crockford
Let's see another example // if no other d is found
// then d is created as a global variable What if the variable existed,
but you didn't realize it? What if another function comes along and
changes your variable unexpectedly? After this code executes, answer == ? b) "outside"
<script>
function test() {
var outside = "outside";
if(true) {
var inside = "inside";
}
return inside;
}
var answer = test();
</script> e) null answer is a b) "outside"
c) undefined
d) false
After this code executes, answer == ? a) "inside"
Why? There is no block scope for if statements in JavaScript answer is a e) null c) undefined
What if the variable existed,
but you didn't realize it? c) undefined
answer is a After this code executes, answer == ? What if another function comes along and
changes your variable unexpectedly? The var keyword was never used in this script a) "inside"
//globals i, ii created
for(i=0, ii=arr.length; ...

//non-global
for(var i=0, ii=arr.length;... <script>
function test() {
inner = "inside";
}
test();
answer = inner;
</script> d) false
e) null b) "outside"
Always remember to use the var keyword
to save yourself a headache After this code executes, answer == ? The "this" keyword is pointing to "obj" Why? a) "inside"
b) "outside"
d) false
Don't forget for loops <script>
var answer = "outside";

var obj = {};
obj.my_value = "inside";
obj.my_function = function() {
answer = this.my_value;
};

obj.my_function();
</script> Why? What if another function comes along and
changes your variable unexpectedly? a) "inside"
d) false
Let's see an example The var keyword was never used in this script Why? <script>
function test() {
inner = "inside";
}
test();
answer = inner;
</script> a) "inside"
// d is declared without var
d = "new value"; Always remember to use the var keyword
to save yourself a headache b) "outside"
// then it checks for d
// as a function parameter
function example(b) { // if no other d is found
// then d is created as a global variable What if the variable existed,
but you didn't realize it? c) undefined
answer is a c) undefined
After this code executes, answer == ? d) false
e) null b) "outside"
Why? // then it checks for d
created outside the function
var a = 7; e) null } The var keyword was never used in this script In JavaScript, variables declared without the var keyword will climb up the scope chain to global namespace. <script>
function test() {
inner = "inside";
}
test();
answer = inner;
</script> After this code executes, answer == ? answer is a //globals i, ii created
for(i=0, ii=arr.length; ...

//non-global
for(var i=0, ii=arr.length;... Don't forget for loops // first it checks for d
// created previously in this scope
var c = 6; You might have expected the "inside" variable
to be unavailable outside the scope it was declared in. Otherwise, they climb up the scope chain,
looking for another variable of the same name If there is no such variable,
then the variable will become global This can be pretty surprising! Bad Stuff can Happen! In many other languages these blocks have their own scope if for while try switch Not in JavaScript catch But this time we used var! Even with var As a result... Some people recommend that you declare all your variables at the beginning of a function function safe_example(arg) {
// one var statement per function
var i, ii, str;
for(i=0, ii = arg.length; i<ii; i++) {
str = arg[i];
}
} Bad Stuff can Happen! Don't create globals accidentally! The Quiz BUT you were wrong
Full transcript