Object-oriented Javascript

Intro

About Us

Ivan Weiler - senior developer and educator, Inchoo
http://inchoo.net/author/weiler/profile/

Osijek Software City, http://softwarecity.hr
J. J. Strossmayer University of Osijek – Department of Mathematics, http://www.mathos.unios.hr/

Workshop URL

http://ivanweiler.github.io/oo-javascript/
(source at https://github.com/ivanweiler/oo-javascript/)

Workshop user guide

Google Chrome with built-in dev tools
https://developers.google.com/chrome-developer-tools/

Codecademy, JSFiddle

http://www.codecademy.com/ Learn to code interactively. Register.
http://jsfiddle.net Online code editor and playground. Register.

Data Structures

Basic types

Array

Array has Keys and Values.
"length" is property of String and Array only.

Array short syntax

Array - example 1

Object

Same as Array but with named keys.

Object short syntax




Object access



Object method



FOR IN statement

Loop through Arrays using for loop and through Objects using for-in loop.
Use Object.hasOwnProperty() to filter if needed (or Object.keys()).

Back to start



Everything is an Object

Global and Element Objects

There are many other built in object like Math, JSON, Date, RegExp ..
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

Also, every html element is an object.

Questions

Todo

CodeAcademy courses - Data structures
http://www.codecademy.com/en/tracks/javascript

Resources

http://www.w3schools.com/js/default.asp
http://www.w3schools.com/jsref/default.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

Prototyping

What is OOP?

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which are data structures that contain data, in the form of fields, often known as attributes or properties; and code, in the form of procedures, often known as methods.

Class vs. Object - An object is an instance of a class

Class Constructor

A new JavaScript class is defined by creating a simple function.
A "constructor" in JavaScript is just a function that happens to be called with the new operator.

Class Constructor - example

Class properties and methods

Prototype allows you to add properties and methods to an object.

Class - example

Every JavaScript object has an internal property called Prototype. If you create a new object via "new SomeClass()", 
the objects Prototype property will be set to the object referenced by "SomeClass.prototype".

Object vs Class

Prototype Chain

Inheritance

Inheritance is when one class extends another class, inheriting all its behaviour (properties and methods).

Prototype Chain

proto (or __proto__) in debugger shows full inheritance chain. It's a pointer to prototype of the class that created that object. If property or method isn't found in current object, engine goes through prototype chain trying to find it.

Prototype Chain

Native objects

All objects in JavaScript are descended from object "Object", so all objects inherit methods and properties from Object.prototype.

Extending native prototypes

One mis-feature that is often used is to extend Object.prototype or one of the other built-in prototypes. While sometimes used by popular frameworks, there is still no good reason for cluttering built-in types with additional non-standard functionality.
The only good reason for extending a built-in prototype is to backport the features of newer JavaScript engines; for example Array.forEach, etc.

Questions

Todo

CodeAcademy courses - Objects I, Objects II
http://www.codecademy.com/en/tracks/javascript

Resources

http://net.tutsplus.com/tutorials/javascript-ajax/prototypes-in-javascript-what-you-need-to-know/
http://net.tutsplus.com/tutorials/javascript-ajax/the-basics-of-object-oriented-javascript/
http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/
http://mckoss.com/jscript/object.htm

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype
http://dmitrysoshnikov.com/ecmascript/javascript-the-core/
http://en.wikipedia.org/wiki/Prototype-based_programming
http://ejohn.org/apps/learn/#64

Value vs. Reference

Example 1

Example 2

Context

Example 1

"this" references current object in execution. Context, the value of this = where are we currently, in which object. 
Context is controlled by either the new keyword, in which case the context is the newly created object, or by how a function is invoked.

Example 2

Example 3

The Window

The window object represents an open window in a browser. In browsers, everything happens inside window. The Matrix has you!

The Window

All global JavaScript objects, functions, and variables automatically become members of the window object.

Changing function context

There is also new Function.bind(thisArg[, arg1[, arg2[, ...]]]).

Namespaces

Code is ussualy grouped in namespaces to seperate functionality and prevent value overwriting.

Wrapping global code in function

Questions

Variable Scope

Global and local variables

If variable isn't set as local with "var", it will always be global in javascript.

The Scope Chain

When variable isn't found, javascript will climb up the scope chain trying to find it.
This is why window object is acting like top most global in javascript.

Local, global, property ..

Hoisting

Variable declarations are hoisted, initializations are not. Declare your variables at the top !!

Function Hoisting

Closures

Every JavaScript function forms a closure on creation. When you pass function to another scope, it takes its variable environment with it.

Closures

https://javascriptweblog.wordpress.com/2010/10/25/understanding-javascript-closures/

Closures - example 1

Closures - example 2

In javascript you're often passing functions as callbacks, this is when closures are really useful.
Test Link

Closures - example 3

Closures - example 4

Questions

Todo

http://ejohn.org/apps/learn/#48
Go through this workshop one more time. Check resources.

Resources

http://ryanmorr.com/understanding-scope-and-context-in-javascript/
http://ejohn.org/apps/learn/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
http://www.w3schools.com/js/js_function_closures.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
https://javascriptweblog.wordpress.com/2010/10/25/understanding-javascript-closures/

2015 Ivan Weiler, Inchoo