21
GameCraft Training Javascript BASICS /*Magic lives here*/ @Rado_g

JavaScript Basics - GameCraft Training

Embed Size (px)

DESCRIPTION

The first lecture about JavaScript that is for internal GameCraft training

Citation preview

Page 1: JavaScript Basics - GameCraft Training

GameCraft Training

Javascript BASICS/*Magic lives here*/

@Rado_g

Page 2: JavaScript Basics - GameCraft Training

Client-side programmingConsole play >_JavaScript 101The DOM TreeJSON {coolness : “maximum”}jQuery (Where things get better)

Agenda

Page 3: JavaScript Basics - GameCraft Training

Object Oriented Programming – Classes, Interfaces, Methods, Scopes

HTML & CSS (concept level)

Some prereqs (What you should already know)

Page 4: JavaScript Basics - GameCraft Training

Executed by the Browser (Client side)Not(d)e – There’s also Server Side JS

Serves for Doing fancy stuff with the HTML Creating richer UI AJAX Doing Web Applications

Prototype based, dynamic, scripting language

JavaScript

Page 5: JavaScript Basics - GameCraft Training

Tools of Trade – Debuggers

Page 6: JavaScript Basics - GameCraft Training

Netbeans v. >= 6.9Aptana StudioJavaScript Development Tools for EclipseMyEclipseVisual Studio (+ Telerik JustCode 4 JavaScript)VIMTextMate (ако ядете ябълки)

Tools of Trade – IDEs

Page 7: JavaScript Basics - GameCraft Training

Variables – no types. Defined with varvar a = 5;var b = “string”; var getRandom = function(arg) { return arg+2; }

Types are associated with values, not with variablestypeof

Arrays – can store different types in one arrayvar arr = [1,2,3,4,5];

Hashes (Maps) – but no Objects as keysvar keyvalue = {name : “Rado”, age : 21, work :

“GameCraft”}

JavaScript 101 - Variables

Page 8: JavaScript Basics - GameCraft Training

Lets have <script type=“text/javascript”>alert(“Hello”)</script> in an html page.

Run the page via a web browser

Let’s view the code - include.html

JavaScript 101 – How to run a simple script

Page 9: JavaScript Basics - GameCraft Training

Also, you can use the src attribute :<script type=“text/javascript” src=“javascript/include.js”></script>

JavaScript files end with .jsYou can include as many JavaScript files as

you wantInclusion order is the order that the <script>

tags were included

Let’s view the code - include2.html + include2.js

JavaScript 101 – How to run a simple script

Page 10: JavaScript Basics - GameCraft Training

We know how toInclude JavaScript files in HTMLRun them in the browserView the output in the consoleDefine simple variables

Next typeof

Recap!

Page 11: JavaScript Basics - GameCraft Training

typeof = Gravity Gun in Half Life 2

Page 12: JavaScript Basics - GameCraft Training

JavaScript is Dynamic Language, but there are types too

Lets have the following code : var calc = function(a,b) { return a+b; } cacl(5,3) 8; calc(“rado”, “rado”) “radorado” calc(true, true) 2 calc(“rado”, true) “radotrue”

What if we want to sum only numbers ? Enter, typeof typeof(5) “number” typeof(“rado”) “string”

Javascript 101 - Types

Page 13: JavaScript Basics - GameCraft Training

Undefined is a special value for undefined variables

var a; typeof(a); // “undefined”typeof(undefined); // “undefined”This is a standart check:

if(typeof(something) !== “undefined”) { … }

Javascript 101 – Types - Undefined

Page 14: JavaScript Basics - GameCraft Training

Arrays are defined without initial sizevar arr = []; // empty array

There are few methods to deal with arrays arr.push(2); // now array has one element arr[0]; // 2 arr[1]; // undefined arr.length; // 1

But, if we want to check for arrays : typeof(arr) === “object”

So there is a way : arr.constructor === Array (But we will talk later

about Prototypes)

JavaScript 101 Arrays

Page 15: JavaScript Basics - GameCraft Training

Functions are something very important in JavaScript

typeof(func) === “function”Functions can :

Take functions as argumentsBe anonymousReturn functionsBe assigned to variablesBe Called from variables

They also create objects and define scopeGo go examples !

JavaScript 101 Functions

Page 16: JavaScript Basics - GameCraft Training

CLOSURES

Page 17: JavaScript Basics - GameCraft Training

Scope = visibility of variablesClosure = function + all variables that are

defined in the function’s scope We can “walk” between different closuresClosures are created by functionsA function can see its parent scopeWindow is the object at the top of the hierarchyExamples – closures.jsMore info -

http://jibbering.com/faq/notes/closures/

Closures && Scope

Page 18: JavaScript Basics - GameCraft Training

Objects && Prototypes

Page 19: JavaScript Basics - GameCraft Training

In JavaScript, there are no classesJavaScript works with PrototypesObjects are created by functionsThe function is the constructorObj.hasOwnProperty() – testing for not

prototype propertiesExamples – prototype.js, number.js,

array_prototype.js, string_prototype.js namespace.js

Objects + Prototypes

Page 20: JavaScript Basics - GameCraft Training

{ description : “XML on diet”, meaning : “JavaScript Simple Object Notation”,usedFor : [{ case : “Create JS objects”} , { case : “Model for data-transportation”, {case : “Configuration files”}

]}

Every big JS library is based on JSON configurations

Examples

JSON (and the Argonaths)

Page 21: JavaScript Basics - GameCraft Training

Always insert ;Never use the with() operator – random

behaviourNever use new Array(). Use [] for creating

arraysAlways use === instead of == (== is

broken)There is no block scope

Declare all variables at the top of functionsAvoid global variables as much as possibleUse new only with InitialCaps variables

JavaScript Pitfalls (Some of them)