Week 1
Day 3
An object is a
collection of properties
A
property is a
named container for a
value
w/ some additional attributes
The
name of a property
is called
a
key
;
thus,
an object
can be considered as
a
collection of key-value pairs
.
There are similar concepts in other programming languages,
e.g.,
Map, Dictionary, Associative Array, Symbol Table, Hash Table
, ...
Dot Notation
var jane = {
name: 'Jane',
'desc.func': function () {
return 'Person named ' + this.name;
},
};
$ jane.name
// 'jane'
$ jane['desc.func']
// [Function]
Bracket Notation
this refers to the object on which the method has been invoked
> var obj = { method: returnThisStrict };
> obj.method() === obj
true
Normal functions in sloppy mode
function returnThisSloppy() {
return this
}
> returnThisSloppy() === window
true
Normal functions in strict mode
function returnThisStrict() {
'use strict';
return this
}
> returnThisStrict() === undefined
true
var counter = {
count: 0,
inc: function () {
this.count++;
}
}
> var func = counter.inc;
> func()
> counter.count // didn’t work
0
> var func3 = counter.inc.bind(counter);
> func3()
> counter.count // it worked!
1
function callIt(callback) {
callback();
}
> callIt(counter.inc)
❌
✓
> callIt(counter.inc.bind(counter))
var obj = {
name: 'Jane',
friends: [ 'Tarzan', 'Cheeta' ],
loop: function () {
'use strict';
this.friends.forEach(
function (friend) { // (1)
console.log(this.name+' knows '+friend); // (2)
}
);
}
};
> obj.loop()
What to do?
loop: function () {
'use strict';
var that = this;
this.friends.forEach(function (friend) {
console.log(that.name+' knows '+friend);
});
}
loop: function () {
'use strict';
this.friends.forEach(function (friend) {
console.log(this.name+' knows '+friend);
}.bind(this)); // (1)
}
this.friends.forEach(function (friend) {
console.log(this.name+' knows '+friend);
}, this);
Array (as Objects)
Array Methods
Map
Reduce
Filter
forEach
...
mutating and non-mutating methods
> var arr = [ 'a', 'b', 'c' ]; // array literal
> arr[0] // get element 0
'a'
> arr[0] = 'x'; // set element 0
> arr
[ 'x', 'b', 'c' ]
Arrays Can Also Have Properties
> var arr = [ 'a', 'b' ];
> arr.foo = 123;
> arr
[ 'a', 'b' ]
> arr.foo
123
> var arr = new Array(2);
> arr.length
2
> arr // two holes plus trailing comma (ignored!)
[ , ,]
// The same as ['a', 'b', 'c']:
var arr1 = new Array('a', 'b', 'c');
// AVOID this.
An empty array with a given length has only holes in it!
The problem is that you can’t create arrays with a single number in them, because that is interpreted as creating an array whose length is the number:
> new Array(2) // alas, not [ 2 ]
[ , ,]
> new Array(5.7) // alas, not [ 5.7 ]
RangeError: Invalid array length
> new Array('abc') // ok
[ 'abc' ]
Adding and Removing Elements (Destructive)
Array.prototype.shift()
Removes the element at index 0 and returns it. The indices of subsequent elements are decremented by 1:
> var arr = [ 'a', 'b' ];
> arr.shift()
'a'
> arr
[ 'b' ]
Array.prototype.unshift(elem1?, elem2?, ...)
Prepends the given elements to the array. It returns the new length:
> var arr = [ 'c', 'd' ];
> arr.unshift('a', 'b')
4
> arr
[ 'a', 'b', 'c', 'd' ]
Array.prototype.pop()
Removes the last element of the array and returns it:
> var arr = [ 'a', 'b' ];
> arr.pop()
'b'
> arr
[ 'a' ]
Array.prototype.push(elem1?, elem2?, ...)
Adds the given elements to the end of the array. It returns the new length
> var arr = [ 'a', 'b' ];
> arr.push('c', 'd')
4
> arr
[ 'a', 'b', 'c', 'd' ]
Array.prototype.reverse()
Array.prototype.sort(compareFunction?)
> var arr = [ 'a', 'b', 'c' ];
> arr.reverse()
[ 'c', 'b', 'a' ]
> arr // reversing happened in place
[ 'c', 'b', 'a' ]
> var arr = ['banana', 'apple', 'pear', 'orange'];
> arr.sort()
[ 'apple', 'banana', 'orange', 'pear' ]
> arr // sorting happened in place
[ 'apple', 'banana', 'orange', 'pear' ]
Array.prototype.concat(arr1?, arr2?, ...)
Array.prototype.slice(begin?, end?)
Array.prototype.join(separator?)
> var arr = [ 'a', 'b' ];
> arr.concat('c', ['d', 'e'])
[ 'a', 'b', 'c', 'd', 'e' ]
> arr
[ 'a', 'b' ]
> [ 'a', 'b', 'c', 'd' ].slice(1, 3)
[ 'b', 'c' ]
> [3, 4, 5].join('-')
'3-4-5'
> [3, 4, 5].join()
'3,4,5'
> [3, 4, 5].join('')
'345'
> [undefined, null].join('#')
'#'
Array.prototype.indexOf(searchValue, startIndex?)
Array.prototype.lastIndexOf(searchElement, startIndex?)
> [ 3, 1, 17, 1, 4 ].indexOf(1)
1
> [ 3, 1, 17, 1, 4 ].indexOf(1, 2)
3
> [NaN].indexOf(NaN)
-1
> [ 3, 1, 17, 1, 4 ].lastIndexOf(1)
3
> [ 3, 1, 17, 1, 4 ].lastIndexOf(1, -3)
1
Examination Methods
Array.prototype.forEach(callback, thisValue?)
Array.prototype.every(callback, thisValue?)
Array.prototype.some(callback, thisValue?)
var arr = [ 'apple', 'pear', 'orange' ];
arr.forEach(function (elem) {
console.log(elem);
});
> function isEven(x) { return x % 2 === 0 }
> [ 2, 4, 6 ].every(isEven)
true
> [ 2, 3, 4 ].every(isEven)
false
> function isEven(x) { return x % 2 === 0 }
> [ 1, 3, 5 ].some(isEven)
false
> [ 1, 2, 3 ].some(isEven)
true
Array.prototype.map(callback, thisValue?)
Array.prototype.reduce(callback, initialValue?)
> [ 1, 2, 3 ].map(function (x) { return 2 * x })
[ 2, 4, 6 ]
> [ 1, 0, 3, 0 ].filter(function (x) { return x !== 0 })
[ 1, 3 ]
function add(prev, cur) {
return prev + cur;
}
console.log([10, 3, -1].reduce(add)); // 12