So far, we've been building objects from scratch using the "object literal" (shortcut) syntax.
Javascript provides another way of building objects using the new
keyword. It looks something like this:
var brandon = new Object();
// The new object works just as if we had
// defined it using "var brandon = {};"
brandon.firstName = 'Brandon';
brandon.lastName = 'Carpenter';
log(brandon.firstName + ' ' + brandon.lastName);
This may not seem very handy at first, but Javascript also provides us with a way of creating "pre-built" objects using the new
keyword. Here are the basic ones:
new Object()
is just like {}
.new Array()
is just like []
.There's also some useful built-in object types that we haven't seen yet:
Exactly the same as creating an array using the "array literal" syntax.
var chores = new Array(
'take out the trash.',
'wash the dishes'
);
/*
Equivalent to:
var chores = [
'take out the trash.',
'wash the dishes'
];
*/
for (var i=0; i < chores.length; i++) {
log('Brandon needs to ' + chores[i]);
}
String objects are not the same as string primitives!
The nice thing about string objects is that they have methods to interact with the string.
var myName = new String('Brandon');
log( myName.toUpperCase() );
Number objects are not the same as number primitives!
The same thing applies for numbers.
var cost = new Number(16.534439432);
log( cost.toFixed(2) );
Boolean objects are not the same as boolean primitives!
The Boolean object is an object that contains a boolean value as one of it's properties.
It is important to note that (since objects are always truthy) any boolean object that you create will evaluate as true in an if statement.
var isThisTrue = new Boolean(false);
log( typeof isThisTrue );
if ( isThisTrue ) {
log('true');
} else {
log('false');
}
log('------------');
// You can retrieve the value stored in the
// object by calling the `valueOf()` method
log( isThisTrue.valueOf() );
Boolean objects are pretty much useless.
JavaScript provides a shortcut for calling methods of these primitive container objects.
If you try to call a method on a primitive, it will automatically convert that primitive to an object behind the scenes so it can call that method.
var myName = new String('Brandon');
log( myName.toUpperCase() );
log( "Brandon".toUpperCase() );
.
var pi = 3.14159;
log( pi.toFixed(2) );
Functions in Javascript are actually objects as well, so you can create one using the new keyword.
However, I don't want you to ever do this, so I'm not even going to show how here.
Dates are super-useful object types in Javascript. You can construct a Date works in one of four ways.
var date1 = new Date( 2012, 05, 23, 15, 35, 29 );
log( "Date 1: " + date1 );
var date2 = new Date( "05/23/2012 15:35:29" );
log( "Date 2: " + date2 );
var date3 = new Date( 1337801729000 );
log( "Date 3: " + date3 );
// No arguments
var now = new Date();
log( "Now: " + now );
var now = new Date();
log( now.toDateString() );
log( now.toTimeString() )
log( now.toUTCString() );
log('---------------');
now.setYear( 2011 );
log( now.toDateString() );
log( now.getDate() );
Regular expressions are a way of matching patterns in strings. They're useful in combination with the replace()
and search()
methods of String objects.
var pattern = new RegExp('[Hh]ello');
var s = 'Hello, world!';
var edited = s.replace(pattern, 'Goodbye');
log(edited);
We don't have time to really go into this, but after you've been programming a while I highly recommend learning how to use them.
Scope is whether or not a particular line of code can access a particular variable.
.
iAmGlobal = 5;
.
log(iAmGlobal);
For example:
var hello = function() {
var greeting = "hello";
log(greeting);
};
hello();
log(typeof greeting);
When a variable is declared in a child scope that is already available in a parent scope it is said to "shadow" or "mask" the other variable.
var greeting = 'goodbye';
var hello = function() {
var greeting = 'hello';
log(greeting);
};
hello();
log(greeting);
.
var greeting = 'goodbye';
var hello = function() {
greeting = 'hello';
log(greeting);
};
hello();
// The above function call modified the
// global greeting variable!
log(greeting);
One trick that advanced Javascript programmers often use is to create a new variable scope using an anonymous function that is executed immediately.
(function() {
// Protect "myvalue" so no one else can edit it!
var myvalue = 100;
log(myvalue);
}());
The above is equivalent to:
var newscope = function() {
// Protect "myvalue" so no one else can edit it!
var myvalue = 100;
log(myvalue);
};
newscope();
For more scope examples in Javascript check out: this post on Stack Overflow.
Table of Contents | t |
---|---|
Exposé | ESC |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Notes | 2 |
Help | h |