Você está na página 1de 15

Introduction to JavaScript

Generalities

JavaScript is NOT Java (Netscape / SUN)

Interpretation vs. compilation (scripting vs. programming)


JavaScript is loosely typed JavaScript can be complex

Every modern browser has a JavaScript interpreter (engine) included


Like HTML/CSS, browsers do have different implementations and deviations of JavaScript language

Lexical Structure

JavaScript is case sensitive

Line breaks and optional semicolon ;


C++ and C-style comments Identifiers can only start with letters, underscore or dollar and can contain only letters, digits, underscore or dollar ([a-zAZ_$][a-zA-Z0-9_$]*) Reserved keywords (e.g. break, instanceof, delete, abstract)

String delimited by " or '


Numbers: decimal, hexadecimal, scientific

Numbers

Integer literals (0, 23, 8567)


Hexadecimal literals (0xff, 0xCAFEA) Floating point literals (2.5,.66, 1.3446e-35) Math object for arithmetic operations Special numbers (e.g. Number.MAX_VALUE, Number.NaN, Number.POSITIVE_INFINITY)

Numbers have specific formatting methods: toFixed, toPrecision, toExponential


Numeric conversion: "5"*"20" ?

Strings

Enclosing strings with " or '

Escaping sequences with backslash \


Concatenate strings with + No need to escape double quotes when enclosing with single quotes and viceversa Strings have specific methods like toUpperCase, substring, indexOf, split, replace

Converting strings to numbers using parseInt and parseFloat

Booleans

2 values: true and false

All types can be converted to booleans 0, NaN, null, undefined, "" are converted to false
Everything else is converted to true

Conversion using object constructor new Boolean()


Conversion using double negation !!

Functions

Function is a type, therefore can be assigned to a variable

Variable number of arguments or arguments do not need explicit declaration


Predefined arguments object

Functions can be considered methods when assigned to an object's property


Every function has a predefined property: prototype object apply and call methods

Objects

An object is a collection of named values (properties)

Create objects using new keyword: new Object()


Create object using literals: { name: 'John' } Reference object's properties using dot: obj.name

Reference object's properties using string index: obj["name"]


Objects (literal) are often used as associative arrays (~hashmap) to store data (JSON)

Arrays

Define arrays using constructor: new Array(), new Array(5), new Array('a',2,'three') Define array literals: ['one','two',3], [1,,,4] Specific methods: join, concat, push, pop, shift, reverse, sort, splice and others Iterate using length property or for/in

null and undefined


null is the value of a variable with no value undefined is the evaluation of an identifier that was never declared or the value of a variable that was never assigned a value

Date

4 ways to instantiate a Date object: new Date() current date/time new Date(milliseconds) given timestamp new Date(string) standard formatted date time new Date(Y,M,D,h,m,s,millis) separate fields Getters and setters each field
Parsing standard time format strings Locale-sensitive formatting to string

Regular Expressions

Using literals: /^[a-z_][0-9a-z_]*$/gi

Using constructor and string: new RegExp("^[a-z_][0-9a-z_]*$","gi")


Usage through RegExp methods exec, test and String methods match, replace, split and search

Errors

JavaScript interpreter throws an Error object when a runtime error occurs Predefined errors types are Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, and URIError Error handling can be done with try / catch / finally Error objects usually have as properties: name and message

Type Conversion

JavaScript tries to convert types used in a context that require other types
Context in which value is used String Number Boolean Object NaN false Error Undefined value "undefined" null "null" 0 false Error Nonempty string As is Numeric value of string or NaN true String object 0 false String object Empty string As is 0 "0" false Number object As is NaN "NaN" false Number object As is "Infinity" true Infinity As is Number object true Negative infinity "-Infinity" As is Number object true Any other number String value of number As is Number object true "true" 1 As is Boolean object false "false" 0 As is Boolean object toString( ) Object valueOf(), toString(), or NaN true As is Value

Types Recap

Difference between "4"*"4" and "4"+"4" typeof(true) and typeof(new Boolean(true)) Type and value of !!new Object() null == undefined typeof(hmm) and typeof(new String(hmm)) 5 != "5" 100 == true new String("coffee") == "coffee" 100 + true NaN == NaN

Você também pode gostar