22
CS 2340 Objects and Design Javascript! Matt Hughes [email protected] Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 1/1

CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

  • Upload
    vubao

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

CS 2340 Objects and DesignJavascript!

Matt [email protected]

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 1 / 1

Page 2: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

What is Javascript?

Extremely popular for web front-end programmingNot Java . . . at allTragically misunderstood (but maybe not)Functional, object-oriented, prototype-based

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 2 / 1

Page 3: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

What is Javascript?

Extremely popular for web front-end programmingNot Java . . . at allTragically misunderstood (but maybe not)Functional, object-oriented, prototype-based

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 2 / 1

Page 4: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Variables do not have static type

This means that you can use a variable to hold anything you like:

var magicallyTransformingVariable = {}; // now it’s an object!magicallyTransformingVariable = []; // now it’s an array!magicallyTransformingVariable = 5; // now it’s a number!

Coming from a language with strict type checking (like Java), this canbe freeing. . . but also dangerous.

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 3 / 1

Page 5: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Let’s unpack that first line

The var keyword is mandatory: you should always declare variableswith var.

var exampleVariable = 5;

If you forget to use var, you’ll make a global variable (which you don’twant).

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 4 / 1

Page 6: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Let’s unpack that first line

The var keyword is mandatory: you should always declare variableswith var.

var exampleVariable = 5;

If you forget to use var, you’ll make a global variable (which you don’twant).

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 4 / 1

Page 7: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Let’s unpack that first line

The var keyword is mandatory: you should always declare variableswith var.

var exampleVariable = 5;

If you forget to use var, you’ll make a global variable (which you don’twant).

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 4 / 1

Page 8: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Built-in types

Not having static type-checking doesn’t mean there are no types:Number

String

Boolean

Array

Object

Null

Undefined

You can ask the type a variable holds with typeof.

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 5 / 1

Page 9: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

The Number type

Unlike in many languages, there is only one variety of number inJavascript. It is analogous to Java’s double.

var sanity;if(0.1 + 0.2 == 0.3) {

sanity = "Verily, we have made contact!";};

After this executes, what is the value of sanity?

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 6 / 1

Page 10: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Number madness

var sanity;if(0.1 + 0.2 == 0.3) {

sanity = "Verily, we have made contact!";};

After this executes, sanity has the value undefined, the defaultvalue for unassigned variables.

To restore sanity, we have to do silly things:

if(10 * 0.1 + 10 * 0.2 == 10 * 0.3) {

(which works because Numbers which are integers behave as such)

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 7 / 1

Page 11: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Number madness

var sanity;if(0.1 + 0.2 == 0.3) {

sanity = "Verily, we have made contact!";};

After this executes, sanity has the value undefined, the defaultvalue for unassigned variables.

To restore sanity, we have to do silly things:

if(10 * 0.1 + 10 * 0.2 == 10 * 0.3) {

(which works because Numbers which are integers behave as such)

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 7 / 1

Page 12: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Objects

var anObject = {};anObject.name = "value";

This creates an empty object and adds into it the pair name : value.We could’ve done this all in one go:

var anObject = {name : "value"};

Javascript objects are at their heart an associative array(or a map, a dictionary).

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 8 / 1

Page 13: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Objects

var anObject = {};anObject.name = "value";

This creates an empty object and adds into it the pair name : value.We could’ve done this all in one go:

var anObject = {name : "value"};

Javascript objects are at their heart an associative array(or a map, a dictionary).

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 8 / 1

Page 14: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Objects

You can do anything you like with objects. . . anything at all. . .The only limit is yourself.

var anObject = {inner : {}};

You can assign anything to any position in objects you have access to.

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 9 / 1

Page 15: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Functions as Objects

Javascript considers functions to be first-class objects.

var aFunction = function whee() {return "Gettin’ pumped about Javascript!";

};

This means that functions can be passed around wherever an objectcould be (which is everywhere).

Notably, you can also attach functions as members of objects:

var anObject = {method : aFunction};

Such functions are called methods.

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 10 / 1

Page 16: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Scope

Function scope is the only notion of scope we have in Javascript.Blocks (like those of Java’s for and if) do not have separatenamespaces.

function aFunction() {var secretPassword = "tacos";

}

After executing this, secretPassword is still undefined.

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 11 / 1

Page 17: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Functionception

Naturally you can put functions inside other functions.Inner functions retain access to variables of their enclosing scope.

function aFunction() {var secretPassword = "tacos";function informationThief() {

return secretPassword;}

}

Here the informationThief has access to secretPassword.

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 12 / 1

Page 18: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Closures

In the last example, our informationThief couldn’t smuggle thepassword out of the enclosing scope.But the informationThief is an object, so we can pass it out of thatfunction’s scope by returning it.

function aFunction() {var secretPassword = "tacos";return function informationThief() {

return secretPassword;}

}

What happens if I execute aFunction()(); ?

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 13 / 1

Page 19: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Encapsulation using closures

Functions used. . . that way. . . are called closures.(There’s a technical definition. I’ll spare you.)The reason why I’m telling you about closures is because they help usachieve encapsulation in Javascript. It is the only way.

function aFunctionYouMightActuallyDefine() {// all of your secretsreturn function publicFunction() {

// whatever you want to let other people do// with your variables

}}

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 14 / 1

Page 20: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

More Fun: Inheritance

I mentioned that Javascript uses something called "prototyping."

Each object has an internal reference to another object, its "prototype".

An object inherits members from its prototype if those members arenot explicitly overridden.

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 15 / 1

Page 21: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

Inheritance

var speaker = "cheese";String.prototype.speakThineName = function() {

return this;};

Now speaker.speakThineName() returns "cheese".We could also do

speaker.speakThineName = function() {return "Heisenberg";

};

which would change the behavior of speaker.speakThineName(),but not anything else.

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 16 / 1

Page 22: CS 2340 Objects and Design - Javascript! · PDF fileFunction scope is the only notion of scope we have in Javascript. Blocks (like those of Java’s for and if) do not have separate

The End

Questions? Thoughts? Lingering feelings of disgust?

Matt Hughes (Georgia Tech) CS 2340 Objects and Design Javascript! 17 / 1