Many folks think they know JavaScript. They know the syntax and can manipulate the DOM. Maybe they have even used jQuery. But do they really know it? I thought I did until I started digging and discovered that JavaScript is more than just a toy language for web pages. It's a full-fledged functional programming language chock full of richness. In this session, targeted at beginners, you will be introduced to some of this richness in an exciting game show format -- complete with prizes. You will learn about functions, objects,
closures, and revealing modules. And you'll leave a more informed JavaScript programmer (unless, of course, you already knew JavaScript in which case you'll just leave with a prize).
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
So You Think You Know JavaScript
1.
2. Guy Royse, Consultant
Pillar Technology
groyse@pillartechnology.com
@guyroyse
Clean Code
TDD
JavaScript
People
Crufty Code
Working Alone
COBOL
Mean People
28. {}
var Foo = function() {
if (!this instanceof Foo) {
return new Foo();
}
this.bar = 42;
this.baz = 'Hello, World!'
};
29. Problem #3
var principal = 5000;
var calculate = function() {
return this.principal * 0.10;
};
var account = { principal : 1000, interest : calculate };
var loan = { principal : 2000, charge : calculate };
var amount = account.interest() + loan.charge();
a)
b)
c)
d)
undefined -- the code will throw an exception
100
500
300
42. Problem #5
varfoo = (function() {
var count = 0;
var values = ['foo', 'bar', 'baz', 'qux'];
return function() {
count++;
return values[count];
};
})();
var results = [foo(), foo(), foo()];
a)
b)
c)
d)
undefined -- the code will throw an exception
['foo','bar','baz']
['bar','baz','qux']
['qux','baz','bar']
Caveats - you’re going to learn some JavaScript stuff here - showing some of the cool features and misfeatures of JavaScript - not about what you should or shouldn't do -- for that you need to use your brain and maybe read some books for some guidance - not about HTML and the DOM
I Thought I Knew JavaScript could write function knew the syntax manipulate the DOM even use jQuery and prototypeBut I was wrong. There is a lot more going on. I had barely scratched the surface. In this session we are going to do more than scratch the surface.Talk will be about advance method invocations, objects and classes, closures, and prototypes if we have time.Discovered some cool stuff in learning JavaScript and today I’m going to share it in…..
Caveats - you’re going to learn some JavaScript stuff here - showing some of the cool features and misfeatures of JavaScript - not about what you should or shouldn't do -- for that you need to use your brain and maybe read some books for some guidance - not about HTML and the DOM
How it works - I present some sample code with multiple outcomes - you select the correct answer - I give out prizes
Everything is an object - even arrays, functions, numbers, strings, booleans
Defining an object - new Object();
Defining an object - this is overkill, probably easier to just do this.
Defining an object - and adding a few properties is easy from here – this is called an object literal or the module pattern
Why did this fail? - we didn't use the new operator - this is a really easy mistake to make - but the error wasn't in the penultimate line, it was in the last line - Policy is just a function - but what happened to what we assigned to this?
The default value for this is the global namespace (i.e. everywhere) - if you are in a browser this is window
This could be really bad for either the bank, or the customerThis is a common mistake and easy to make and it occurs because classes are really just a fiction in JavaScript.
What we really have is a specialized constructor function
By convention always capitalize class names - this is just code, nothing special – anything could go in here - this is defined as the object instance that was created
- this is what is really happening - there is actually a bit more happening here involving prototypes but we'll not get into that today
Function calls are easy to define - function keyword
Function calls are easy to define- assigning it to a variable - a bit more honest -- because they are easy to reassign
Function calls are easy to define - because they are easy to reassign
This is the key to invoking functions
Function calls are easy - just use () at the end of a variable
Function calls are easy - you can call functions on an object or in an array this way too
You can even call them on the fly using immediately executing functions
These are cool, but they can not work as expected so be careful
This code illustrates the problem – calling bar will always return 'Marco!'
Functions can be passed as arguments - create for using the strategy pattern
Functions can be passed as arguments - create for using the strategy pattern
A closure is a bit of code and variables that should otherwise be out of scope
Closures are great for private variables in JavaScript - policy number is now read only and not modifiable for this object
AKA the revealing module patternCombining closures and immediately invoked functions makes for wicked cool singletons - why, then we don't need a function to construct the object - this keeps the global namespace clean - and keeps others from calling code you don't intend to be called - it's the same as a protected constructor in Java
Functions can be passed as arguments - create for using the strategy pattern