Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Effecitive JavaScipt Workshop


Smartbar Smartbar

on 21 January 2010

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Effecitive JavaScipt Workshop

Effecitive JavaScipt Workshop Authors: Martin Lasak
Lukasz Radziwonowicz Grammar Comments use // instead of /* */
Numbers only one type: Number
NaN, isNaN();
Example: 12e2, 1, 1.0, Infinity
number methids: Math object Strings 16 bit Unicode: "A" === "\u00041"
length property
are immutable - changes to String object caused generation of new String object
"c" + "a" + "t" === "cat" Statements
var x = 42;
curly braces do not create new scope, variables should be defined at the top of the function
Falsy values:
false, null, undefined, '' (empty string), 0, NaN (Not-a-Number),
Falsy objects: null, undefined
Falsy literals: '', 0, false
NaN is not equaly to anything, even not to NaN
(false !== null, null===null, but exception NaN!==NaN)
all other values are truthy, including true, the string 'false', and all objects.
for in loop,enumerates the property names (or keys) of an object, test with object.hasOwnProperty(variable)
return; is equal to return undefined;
equality, value and type - === / !==
typeof are 'number', 'string', 'boolean', 'undefined', 'function',and 'object'
NOTICE!!! typeof returns string values.
The && and || operator produces the value of it s first operand if the firsto perand is falsy. Otherwise, it produces the value of the second operand. => produced value is not always boolean value Objects Tips prototype linkage reduce object initialization time and memory consumption.
property name as String, "first-name" allowed but better "firstName" or "first_name"
default property value:
var middle = person["middle-name"] || "(none)";
var status = flight.status || "unknown";
Objects are passed around by reference. They are never copied.
refer to different objects:
var a = {}, b = {}, c = {};
refer the same object:
a = b = c = {};
Prototype prototype chain: delegation
The prototype relationship is a dynamic relationship.
This also works for functions , arrays, strings, numbers, regular expressions, and booleans. Reflection typeof
hasOwnProperty(), see Satement Enumeration in case for in statement for objects, filter out 'functions':
typeof obj[key] !== 'function'
it is best to avoid the for in statement entirely and instead make an array containing the names of the properties in the correct order
Delete Removing a property from an object may allow a property from the prototype linkage to shine through: Global Abatement to minimize the use of global variables is to create a single global variable for your application:
var MYAPP = {};
MYAPP.stooge = {'test':5}; Functions An inner function also enjoys access to the parameters and variables of the functions it is nested within. The function object created by a function literal contains a link to that outer context. This is called closure. This is the source of enormous expressive power. Invocation this and arguments The Method Invocation Pattern function is a property of an object, we call it method
this parameter is a reference to the object itselfs.
The binding of this to the object happens at invocation time. The Function Invocation Pattern function is not a property of the object
this is bound to global object
The Constuctor Invocation Pattern If a function is invoked with the new prefix, then a new object will be created with a hidden link to the value of the function’s prototype member, and this will be bound to that new object.
is not recommended.
If the function was invoked with the new prefix and the return value is not an object, then this (the new object) is returned instead => Singelton Pattern The Apply Invocation Pattern every function contains the apply construct
apply is a method with two parameters: an object to bind this to and an array of parameters
ToDo (expresion):
(2).plus(2); // correct
2.plus(2); // error
var func = function(){};
function func(){};
Scope Scope in a programming language controls the visibility and life times of variables and parameters
scope chain
it is best to declare all of the variables used in a function at the top of the function body.
JavaScript does have function scope. That means that the parameters and variables defined in a function are not visible outside of the function, and that a variable defined anywhere within a function is visible everywhere within the function. Closure A closure is a way to access and manipulate external variables from within a function.
One of the most common uses of closures is to encapsulate some information as a "private variable" (properties of the object that are hidden from outside uses).
callbacks and timers Modules A module is a function or object that presents an interface but that hides its state and implementation
The module pattern takes advantage of function scope and closure to create relationships that are binding and private.
Module Pattern => see JavaScript Best Practice p31
the function has access to the context in which it was created. This is called closure. (modul...return{})
The general pattern of a module is a function that defines private variables and functions; creates privileged functions which, through closure, will have access to the private variables and functions; and that returns the privileged functions or stores them in an accessible place.
Use of the module pattern can eliminate the use of global variables. It promotes information hiding and other good design practices. It is very effective in encapsulating applications and other singletons. Memoization Functions can use objects to remember the results of previous operations, making it possible to avoid unnecessary work. Inheritance Prototypal Classical an object constructor can inherit methods from one other object, creating a prototype object from which all other new objects are built.
prototypes do not inherit their properties from other prototypes or other constructors; they inherit them from physical objects.
Full transcript