1 of 24

JS Basics

Jan Filipowski

2 of 24

Me, myself and I

  • work: Arkency
  • blog: http://agiler.net
  • twitter: @janfilipowski
  • github: yashke

3 of 24

Goals

  • for noobs: teach basics
  • show "hidden" features
  • explain objects model

4 of 24

Values

  • number
  • boolean
  • string
  • null
  • undefined

5 of 24

Type conversions

  • number to string - "" + number
  • string to number - +"1.1", parseInt, parseFloat
  • object to string - object.toString method

6 of 24

Variables

  • defined via var keyword
  • scoped to current function (!) or global
  • in JS 1.7 block scoping with let
  • naming: [a-zA-Z_\$][a-zA-Z_\$0-9]+ with support for unicodes

7 of 24

Global variables and constants

window.variable = value

const constant = value

8 of 24

Literals

  • Array: []
  • Object: {} - keys are strings or numbers
  • String: "" / '' - unicode
  • Boolean: true / false
  • Regular expression: //

9 of 24

Numbers

  • no distinction between integers and floats
  • notation:
    • 123 - decimal
    • 0123 - octal
    • 0x123 - hexadecimal
    • 1.1 - float
    • 1.1e-2 - exponential

10 of 24

Expressions

  • assignment
  • comparison
  • arithmetic
  • bitwise
  • logical
  • string
  • special

11 of 24

Special

  • conditional: condition ? if true : if false
  • comma
  • delete
  • in
  • instanceof
  • new
  • this
  • typeof
  • void

12 of 24

Regular expression

  • RegExp constructor
  • or // literal

13 of 24

Statements

  • block
  • if / else
  • switch
  • while / do ... while
  • for
  • for ( ... in ... )
  • for each ( ... in ... ) [JS 1.6]
  • break
  • continue
  • label (label: while ...)
  • try / catch / finally

14 of 24

Exceptions

  • in 1.5 there's only one catch section.
  • every object can be thrown as exception
  • good practice - exception should have name and description properties
  • defaults: Error, EvalError, InternalError, RangeError, ReferenceError, StopIteration (1.8), SyntaxError, TypeError, URIError

15 of 24

Functions

  • according to ECMAScript they should be declared in scope of function or global
  • function has access to its own variables and to which its parent has access to.
  • special variable: arguments
  • can be named or anonymous
  • can have properties (fields and methods)

16 of 24

Objects

  • definition: collection of properties, where property is name and its value
  • object initialization possibilities:
    • new Object()
    • Object.create(prototype, properties)
    • {}
    • constructor function (class-like)

17 of 24

Object model

  • based on objects only
  • so object inherits from object - prototype (not class)
  • each object has prototype, accessible with __proto__
  • each constructor has prototype for constructed objects - accessible with prototype

18 of 24

Property lookup in prototypical inheritance (console(o.field))

  1. if o has own property 'field' it returns it
  2. if not, it checks if its prototype have such
  3. if yes - returns it
  4. if no - returns undefined

If property in prototype is method, which uses other property of object it starts looking for it's value from original (first) object, not the current one - prototype.

19 of 24

Consequences - examples

  • change prototype object
  • change object

20 of 24

Object construction flow

Assume we have Foo constructor and we want to create object o. Then flow looks like this:

o = new Object()

o.__proto__ = Foo.prototype

o.Foo()

21 of 24

How to pass arguments to prototype constructor

Assume: Bar.prototype = new Foo()

function Bar(name, smth) {

Foo.call(this, name)

this.smth = smth;

}

22 of 24

Setters and getters

No kidding, guys!

23 of 24

Source

24 of 24

KTHXBYE

Questions?