-
Are the objects we have available in the global scope (primitive objects)
-
We can divide them in 3 groups
- Objects containers of data: Object, Array, Function, Boolean, Number
- Objects of utilities: Math, Date, RegExp
- Objects of errors: Error
##Object
-
Object is the father of all the javascript objects, this is, every object inherites from him
-
To create an empty object we can use:
- The literal notation :
var o = {}
- The constructor function Object():
var o = new Object();
- The literal notation :
-
Any object contains the following properties and methods:
- The property
o.constructor
containing the constructor function - The method
o.toString()
that returns the object converted into text - The method
o.valueOf()
that returns the value of the object (usuarllyo
)
- The property
>>> var o = new Object();
>>> o.toString()
“[object Object]”
>>> o.valueOf() === o
true
##Array
-
To create arrays we can use:
- Literal notation:
var a = []
- Constructor function Array():
var o = new Array();
- Literal notation:
-
We can pass parameters to the constructor
Array()
- Several parameters: Will be assigned as elements in the array
- A number: Will be considered as the size of the array
>>> var a = new Array(1,2,3,'four');
>>> a;
[1, 2, 3, "four"]
>>> var a2 = new Array(5);
>>> a2;
[undefined, undefined, undefined, undefined, undefined]
- Because of the arrays are objects, we also have available all the methods and properties of the father
Object()
>>> typeof a;
"object"
>>> a.toString();
"1,2,3,four"
>>> a.valueOf()
[1, 2, 3, "four"]
>>> a.constructor
Array()
-
Arrays have the property
length
- Returns the size of the array (number of elements)
- We can modify it and change the size of the array
>>> a[0] = 1;
>>> a.prop = 2;
>>> a.length
1
>>> a.length = 5
5
>>> a
[1, undefined, undefined, undefined, undefined]
>>> a.length = 2;
2
>>> a
[1, undefined]
Arrays have a few interesting methods:
push()
pop()
sort()
join()
slice()
splice()
>>> var a = [3, 5, 1, 7, 'test'];
>>> a.push('new')
6
>>> a
[3, 5, 1, 7, "test", "new"]
>>> a.pop()
"new"
>>> a
[3, 5, 1, 7, "test"]
>>> var b = a.sort();
>>> b
[1, 3, 5, 7, "test"]
>>> a
[1, 3, 5, 7, "test"]
>>> a.join(' is not ');
"1 is not 3 is not 5 is not 7 is not test"
>>> b = a.slice(1, 3);
[3, 5]
>>> a
[1, 3, 5, 7, "test"]
>>> b = a.splice(1, 2, 100, 101, 102);
[3, 5]
>>> a
[1, 100, 101, 102, 7, "test"]
##Function
- Functions are objects
- We can create functions with the constructor function
Function()
(althiugh this method is not recomended as it internally uses an eval() )
Example:
>>> function sum(a, b) {return a + b;};
>>> sum(1, 2)
3
>>> var sum = function(a, b) {return a + b;};
>>> sum(1, 2)
3
>>> var sum = new Function('a', 'b', 'return a + b;');
>>> sum(1, 2)
3
- Functions have the following properties:
- The property
constructor
that contains a reference to the constructor function Function() - The property
length
that contains the number of parameters accepted by the function - The property
caller
(no standard) that contains a reference to the function that called that function - The property
prototype
that contains an object:- It's only useful when we use this function as a constructor
- All objects created by a constructor function maintain a reference to its property
prototype
and can use its properties as if they're of their own
- The property
>>> function myfunc(a){ return a; }
>>> myfunc.constructor
Function()
>>> function myfunc(a, b, c){ return true; }
>>> myfunc.length
3
>>> function A(){ return A.caller; }
>>> function B(){ return A(); }
>>> B()
B()
var some_obj = {
name: 'Ninja',
say: function(){
return 'I am a ' + this.name;
}
}
>>> function F(){}
>>> typeof F.prototype
"object"
>>> F.prototype = some_obj;
>>> var obj = new F();
>>> obj.name
"Ninja"
>>> obj.say()
"I am a Ninja"
- Functions have the following methods:
>>> function myfunc(a, b, c) {return a + b + c;}
>>> myfunc.toString()
"function myfunc(a, b, c) {
return a + b + c;
}"
var some_obj = {
name: 'Ninja',
say: function(who){
return 'Haya ' + who + ', I am a ' + this.name;
}
}
>>> some_obj.say('Dude');
"Haya Dude, I am a Ninja"
>>> my_obj = {name: 'Scripting guru'};
>>> some_obj.say.call(my_obj, 'Dude');
"Haya Dude, I am a Scripting guru"
some_obj.someMethod.apply(my_obj, ['a', 'b', 'c']);
some_obj.someMethod.call(my_obj, 'a', 'b', 'c');
- Functions have available the object
arguments
that (besideslength
) have the propertycallee
that contains a reference to the function called (to itself)
>>> function f(){return arguments.callee;}
>>> f()
f()
(
function(count){
if (count <= 5) {
console.log(count);
arguments.callee(++count);
}
}
)(1)
##Boolean
- The object Boolean is a container for a boolean type value
We can create Boolean objects with te constructor function
Boolean()
Example:
>>> var b = new Boolean();
>>> typeof b
"object"
>>> typeof b.valueOf()
"boolean"
>>> b.valueOf()
false
- The function
Boolean
used as a normal function (withoutnew
) returns the value passed as a parameter converted to boolean
Example:
>>> Boolean("test")
true
>>> Boolean("")
false
>>> Boolean({})
true
##Number
- The function
Number()
can be used:- As a normal function to convert values to numbers
- As a constructor function (with
new
) to create objects
- The number objects have the following methods available:
toFixed()
,toPrecision()
ytoExponential()
Example:
>>> var n = new Number(123.456)
>>> n.toFixed(1)
"123.5"
>>> (12345).toExponential()
"1.2345e+4"
- The method
toString()
of a number object allow us to convert any number to a specific base
Example:
>>> var n = new Number(255);
>>> n.toString();
"255"
>>> n.toString(10);
"255"
>>> n.toString(16);
"ff"
>>> (3).toString(2);
"11"
>>> (3).toString(10);
"3"
##String
- We can create String objects with the constructor function
String()
A String object is NOT a String primitive type of data (valueOf()
)
Example:
>>> var primitive = 'Hello';
>>> typeof primitive;
"string"
>>> var obj = new String('world');
>>> typeof obj;
"object"
>>> Boolean("")
false
>>> Boolean(new String(""))
true
- A string object is similar to an array of characters:
- Every character has an indexed position
- It has available the property
length
Example:
>>> obj[0]
"w"
>>> obj[4]
"d"
>>> obj.length
5
- Although the methods belong to the
String
object, we can use them directly in the primitive type of data string (the object is created internally)
Example:
>>> "potato".length
6
>>> "tomato"[0]
"t"
>>> "potato"["potato".length - 1]
"o"
-
The String objects have available the following methods:
-
toLowerCase()
returns the string converted to lowercase -
charAt()
returns the character found at the specified position -
indexOf()
looks for a text string inside of another text string and returns the position where it was found- If it doesn't find anything it returns -1
-
lastIndexOf()
starts the search from the end of the string- If it doesn't find anything it returns -1
That's whay the proper way of checking if a string exists inside of another string is →
if ( s.toLowerCase().indexOf('couch') !== -1 ) {...}
-
>>> var s = new String("Couch potato");
>>> s.toUpperCase()
"COUCH POTATO"
>>> s.toLowerCase()
"couch potato"
>>> s.charAt(0);
"C"
>>> s.indexOf('o')
1
>>> s.lastIndexOf('o')
11
>>> s.slice(1, 5)
"ouch"
>>> s.split(" ")
["Couch", "potato"]
>>> s.concat("es")
"Couch potatoes"
##Math
-
Math is an object with properties and methods useful for mathematical work It is NOT a constructor of other objects
-
Some interesting
Math
methods are:
##Date
Date()
is a constructor function that creates date objects We can create new Date objects by passing:- nothinga (will take the current date as default)
- A date in string format
- separated values that represent: Year, Month (0-11), Day (1-31), Hour (0-23), Minutes (0-59), Seconds (0-59) y Miliseconds (0-999)
- A timestamp value
Example (Firebug shows the result of the toString
method over a date object):
>>> new Date()
Tue Jan 08 2008 01:10:42 GMT-0800 (Pacific Standard Time)
>>> new Date('2009 11 12')
Thu Nov 12 2009 00:00:00 GMT-0800 (Pacific Standard Time)
>>> new Date(2008, 0, 1, 17, 05, 03, 120)
Tue Jan 01 2008 17:05:03 GMT-0800 (Pacific Standard Time)
>>> new Date(1199865795109)
Wed Jan 09 2008 00:03:15 GMT-0800 (Pacific Standard Time)
-
Some methods to work with date objects are:
-
setMonth()
ygetMonth()
write and read the month in a date object each one of them (we have available the same kind of methods for year, day, hours, minutes, etc…) -
parse()
given a string, it returns its timestamp -
UTC()
produces timestamp given some month, year, day, etc.. -
toDateString()
returns the content of a date object in american format
-
Example:
>>> var d = new Date();
>>> d.toString();
"Wed Jan 09 2008 00:26:39 GMT-0800 (Pacific Standard Time)"
>>> d.setMonth(2);
1205051199562
>>> d.toString();
"Sun Mar 09 2008 00:26:39 GMT-0800 (Pacific Standard Time)"
>>> d.getMonth();
2
>>> Date.parse('Jan 1, 2008')
1199174400000
>>> Date.UTC(2008, 0, 1)
1199145600000
>>> var d = new Date(2012, 5, 20);
>>> d.getDay();
3
>>> d.toDateString();
"Wed Jun 20 2012"