programming:javascript
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| programming:javascript [2023/11/01 07:31] – removed - external edit (Unknown date) 127.0.0.1 | programming:javascript [2023/11/01 07:31] (current) – ↷ Page moved from camunda:programming:javascript to programming:javascript skipidar | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ==== Javascript Programming ==== | ||
| + | ==== APIs ==== | ||
| + | Javascript is composed of libraries and different APIs. | ||
| + | The core API is called " | ||
| + | Tthere are several different other APIs which are implemented by different Browsers, or may be loaded optionally like " | ||
| + | More about it : https:// | ||
| + | |||
| + | Some APIs which you have to know, when programming JavaScript. | ||
| + | |||
| + | |Javascript Web API | Available when programming in browser | ||
| + | | JQuery API | Available as an optional lib. Allows to handle DOM much easier | ||
| + | |||
| + | ==== IDE ==== | ||
| + | |Visual Studio 2013|<fc # | ||
| + | |Aptana IDE|<fc # | ||
| + | |||
| + | |||
| + | ==== Materials ==== | ||
| + | |||
| + | |Shortcuts for VisualStudio 2013| http:// | ||
| + | |||
| + | |||
| + | ==== Glossary ==== | ||
| + | |Variable hoisting|< | ||
| + | <sxh javascript> | ||
| + | // functions are equivalent because of variable hoisting | ||
| + | javascript> | ||
| + | javascript> | ||
| + | </ | ||
| + | |Closures|< | ||
| + | < | ||
| + | // function returns a function which uses internal variables! | ||
| + | function getClosure(name) { | ||
| + | | ||
| + | // private variable | ||
| + | var greetings = "Hello " | ||
| + | | ||
| + | //anonymous function | ||
| + | return function (name) { | ||
| + | console.log(greetings); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // create functions at runtime! | ||
| + | var closureCreatedAlexFunction = getClosure(" | ||
| + | var closureCreatedGunillaFunction = getClosure(" | ||
| + | |||
| + | closureCreatedAlexFunction(); | ||
| + | closureCreatedAlexFunction(); | ||
| + | </ | ||
| + | |Function in JS |< | ||
| + | - it's an **object** | ||
| + | - it may be a **method** - if it's within another function | ||
| + | - it may be a **constructor** - if it is called with prefix " | ||
| + | </ | ||
| + | |Prototypes|< | ||
| + | <sxh javascript> | ||
| + | // JS is a class-less prototype-based language. It doesn' | ||
| + | |||
| + | /* functions may become constructors, | ||
| + | - they return nothing or a primitive | ||
| + | - they are called with the " | ||
| + | then they return an object, made from the same prototype, as constructor-function | ||
| + | */ | ||
| + | |||
| + | // has prototype Human.prototype | ||
| + | function Human(name) { | ||
| + | this.name = name; | ||
| + | } | ||
| + | |||
| + | // has prototype Human.prototype | ||
| + | var alex = new Human(" | ||
| + | console.log(" | ||
| + | |||
| + | // prototypes are accessible. They may be extended. Then all the new Humans will inherit the extended functions! | ||
| + | Human.prototype.sayName = function () { console.log(this.name) } | ||
| + | alex.sayName(); | ||
| + | |||
| + | // Object2 -> Object1 -> Object1.prototype | ||
| + | var Object1 = {name:" | ||
| + | var Object2 = Object.create(Object1); | ||
| + | </ | ||
| + | |Constuctors|< | ||
| + | <sxh javascript> | ||
| + | /* | ||
| + | this function is a constructor! | ||
| + | but it may be called without new... then " | ||
| + | and this.name=" | ||
| + | |||
| + | So - check the instance of this and redirect function calls to Object creation | ||
| + | */ | ||
| + | function Human(name) { | ||
| + | // check " | ||
| + | if(!(this instanceof Human)){ | ||
| + | return new Human(name); | ||
| + | } | ||
| + | this.name = name; | ||
| + | } | ||
| + | var obj1 = new Human(" | ||
| + | var obj2 = Human(" | ||
| + | console.log(obj1); | ||
| + | console.log(obj2); | ||
| + | |||
| + | |||
| + | </ | ||
| + | ==== Fallpits ==== | ||
| + | |||
| + | * Don't use ' | ||
| + | * Eval is evil. It enables injections. | ||
| + | * Always use ' | ||
| + | |||
| + | == Importing Javascript Files == | ||
| + | * close tag explicitely by **</ | ||
| + | * Insert attributes: **language=" | ||
| + | |||
| + | < | ||
| + | <script language=" | ||
| + | </ | ||
| + | |||
| + | == Semicolon is set automatically every line end == | ||
| + | |||
| + | <sxh javascript> | ||
| + | // returns an object | ||
| + | function foo(){ | ||
| + | | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | // returns ' | ||
| + | function foo(){ | ||
| + | | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Equality | ||
| + | <sxh javascript> | ||
| + | console.log(" | ||
| + | console.log("' | ||
| + | console.log(" | ||
| + | console.log(" | ||
| + | console.log(" | ||
| + | console.log(" | ||
| + | console.log(" | ||
| + | console.log(" | ||
| + | </ | ||
| + | |||
| + | == NaN == | ||
| + | <sxh javascript> | ||
| + | //NaN - not a number | ||
| + | console.log(" | ||
| + | console.log(' | ||
| + | console.log(' | ||
| + | console.log(' | ||
| + | console.log(' | ||
| + | </ | ||
| + | |||
| + | == bitlogic == | ||
| + | <sxh javascript> | ||
| + | // JS will always convert to int first, before applying bit logic | ||
| + | // in JS Negation is a bit logic operator | ||
| + | /* | ||
| + | 3 = 0000 0000 0000 0000 0000 0000 0011 = (int)3.346346 | ||
| + | ~3 = 1111 1111 1111 1111 1111 1111 1100 = -4 | ||
| + | -3 = 1111 1111 1111 1111 1111 1111 1101 | ||
| + | ~-3 = 0000 0000 0000 0000 0000 0000 0010 = 2 | ||
| + | */ | ||
| + | |||
| + | console.log(' | ||
| + | console.log(' | ||
| + | |||
| + | console.log(' | ||
| + | console.log(' | ||
| + | console.log(' | ||
| + | </ | ||
| + | |||
| + | **Achtung: | ||
| + | |||
| + | |||
| + | == Conversion == | ||
| + | - JS enterpritates from left to right | ||
| + | - JS tries to convert String to NUmber and Number to String | ||
| + | <sxh javascript> | ||
| + | 12 + “ or ” + “20”; // “12 or 20” | ||
| + | “12” / 2 + 1; // 7 | ||
| + | “day” * 2; // NaN | ||
| + | var a = 42; // 42 | ||
| + | a.toString(); | ||
| + | a.toString().length; | ||
| + | a.toString().length.toString(); | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Inheritance == | ||
| + | {{http:// | ||
| + | |||
| + | |||
| + | |||
| + | ==== Javascript in Browser ==== | ||
| + | The Objects on a webpage are hierarchically organized. | ||
| + | Details: http:// | ||
| + | |||
| + | * **Window object**: Top of the hierarchy. It is the outmost element of the object hierarchy. | ||
| + | * **Document object**: Each HTML document that gets loaded into a window becomes a document object. The document contains the content of the page. | ||
| + | * **Form object**: Everything enclosed in the < | ||
| + | * **Form control elements**: The form object contains all the elements defined for that object such as text fields, buttons, radio buttons, and checkboxes. | ||
| + | |||
| + | {{http:// | ||
