typeof
with
and eval
false
, null
, undefined
, NaN
==
and !=
// get
var name = object.name;
var name = object['name'];
// set
object.name = value;
object['name'] = value;
// delete
delete object.name;
delete object['name'];
var emptyObject = { };
var myObject = {
'some property': 'some value',
foo: bar,
method: function(param) {
// do stuff
}
};
Object.prototype
var Person = function (name) {
this.name = name;
};
Person.prototype.sayHello = function () {
alert('Hello, my name is ' + this.name);
};
var thomas = new Person('Thomas');
thomas.sayHello();
var Person = function (name) {
this.name = name;
};
Person.prototype.sayHello = function () {
alert('Hello, my name is ' + this.name);
};
var thomas = new Person('Thomas');
thomas.sayHello();
Person.prototype.sayMyName = function () {
alert('Hello, my name is ' + this.name);
};
thomas.sayMyName();
var Person = function () {
};
Person.prototype.sayHello = function () {
alert('Hello, my name is ' + this.name);
};
var Customer = function (name) {
this.name = name;
};
Customer.prototype = new Person();
var customer = new Customer('Customer');
customer.sayHello();
alert(parseInt('123'));
alert(parseInt('010'));
parseInt('123'); // 123
parseInt('010'); // 8
parseInt('123'); // 123
parseInt('010'); // 8
parseInt('010', 10);
parseInt('11', 2);
parseInt('123'); // 123
parseInt('010'); // 8
parseInt('010', 10); // 10
parseInt('11', 2); // 3
true
or false
0
, ""
, NaN
, null
and undefined
are falsy &&
, ||
and !
var array = ['one', 'two', {number: 'three'}, function() {}];
var a = ['one', 'two'];
alert(a.length);
a[100] = 'three';
alert(a.length);
array.length
is always one more than the highest indexvar date = new Date('January 25, 2012');
date.getYear(); // 2012
date.getMonth(); // 0 ???
date.getDate(); // 25
Date
function is based on Java's Date class...if (myString.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
// Great Success!
}
function
// Create a variable called add and store a function
// in it that adds two numbers
var add = function(a, b) {
return a + b;
};
// alternatively (but not quite the same as we'll see later)
function add(a, b) {
return a + b;
}
this
is initializedthis
is bound to that object (late binding)var myObject = {
value: 0,
increment: function(inc) {
this.value += inc;
alert(this);
}
}
myObject.increment(2);
this
is bound to the global objectvar foo = function() {
alert(this); // Window in the browser
};
foo();
This was a design mistake in the language.
A consequence is that a method cannot employ an inner function to help it do its work because the inner function does not share the method's access to the object as its this
is bound to the wrong value
myObject.double = function() {
var helper = function() {
this.value = add(this.value, this.value); // this.value
// is undefined
};
helper(); // invoke helper as a function
};
myObject.double(); // invoke double as a method
myObject.double = function() {
var that = this; // workaround
var helper = function() {
that.value = add(that.value, that.value);
};
helper(); // invoke helper as a function
};
myObject.double(); // invoke double as a method
new
prefix, a new object will be created with a hidden link to the value of the function's prototype
memberthis
is bound to that new object// Create a constructor function called Foo
var Foo = function(string) {
this.value = string;
alert(this);
};
// Make an instance of Foo
var myFoo = new Foo('confused');
// Call Foo as an function
var myFoo = Foo('confused');
apply
method lets us construct an array of arguments to use to invoke a functionthis
var context = {};
var args = [3, 4];
var sum = add.apply(context, args); // this === context inside
// the add function
var sum = add.call(context, 3, 4); // this === context inside
// the add function
var myvar = 'my value';
function bar() {
alert(myvar);
var myvar = 'local value';
}
bar();
var myvar = 'my value';
function bar() {
var myvar = undefined;
alert(myvar); // undefined
myvar = 'local value';
}
bar();
alert(add(2, 3));
var add = function(a, b) {
return a + b;
};
alert(add(2, 3));
function add(a, b) {
return a + b;
}
var add = undefined;
alert(add(2, 3)); // error
add = function(a, b) {
return a + b;
};
var add = function add(a, b) {
return a + b;
};
alert(add(2, 3)); // 5
var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
alert(foo);
}
bar();
var foo = 1;
function bar() {
var foo = undefined;
if (!foo) { // true
foo = 10;
}
alert(foo); // 10
}
bar();
function bar() {
x = 10;
}
bar();
alert(x);
function foo() {
var y = 10;
}
foo();
alert(y);
function bar() {
x = 10; // Creates a global variable
}
bar();
alert(x); // 10
function foo() {
var y = 10; // Creates a local variable
}
foo();
alert(y); // y is undefined
function foo(x) {
var tmp = 3;
function bar(y) {
alert(x + y + (++tmp));
}
bar(10);
}
foo(2);
foo(2);
foo(2);
Whenever you see the function keyword within another function, the inner function has access to variables in the outer function. This will always alert 16, because bar can access the x which was defined as an argument to foo, and it can also access tmp from foo.
function foo(x) {
var tmp = 3;
function bar(y) {
alert(x + y + (++tmp)); // 2 + 10 + 4 = 16
}
bar(10);
}
foo(2);
foo(2);
foo(2);
That is not a closure. A closure is when you return the inner function. The inner function will close-over the variables of foo before leaving.
function foo(x) {
var tmp = 3;
return function (y) {
alert(x + y + (++tmp));
}
}
var bar = foo(2); // bar is now a closure
bar(10);
bar(10);
bar(10);
bar(10);
This function will also alert 16, because bar can still refer to x and tmp, even though it is no longer directly inside the scope. However, since tmp is still hanging around inside bar's closure, it is also being incremented. It will be incremented each time you call bar.
(function () {
var privateVariable;
function privateFunction(x) {
// access privateVariable
}
GLOBAL.firstMethod = function (a, b) {
// access privateVariable
};
GLOBAL.secondMethod = function (c) {
// call privateFunction()
};
}());
var singleton = (function () {
var privateVariable;
function privateFunction(x) {
// access privateVariable
}
return {
firstMethod: function (a, b) {
// access privateVariable
},
secondMethod: function (c) {
// call privateFunction()
}
};
}());
(function (window, undefined) {
var document = window.document,
$ = jQuery = window.jQuery.noConflict(),
_ = window._.noConflict(),
Backbone = window.Backbone.noConflict();
var root = {};
// ...
window.root = root;
} (window));
// Silent error
return
{
ok: false
};
// Works well
return {
ok: true
};
return
{
ok: false
};
return; // semicolon insertion
{
ok: false
};
return;
{ // block
ok: false
};
return;
{
ok: false // label
};
return;
{ // useless
ok: false // expression
}; // statement
return;
{
ok: false; // semicolon insertion
};
return;
{
ok: false;
}; // empty statement
return;
{ // unreachable statement
ok: false;
}
===
and !==
, not ==
or !=
if
and while
new
prefix should start with a capital letter{}
instead of new Object()
. Use []
instead of new Array()
eval
is Evilwith
@thedersen
thedersen@gmail.com
http://github.com/thedersen
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Run code sample | r |
Help | h |