Object Oriented and Functional Programming
constructor
var Car = function(wheels, seats, engines) {
//Change this constructor
this.wheels = wheels;
this.seats = seats;
this.engines = engines;
};
public & private
public: this
private: var
var Car = function() {
// this is a private variable
var speed = 10;
// these are public methods
this.accelerate = function(change) {
speed += change;
};
this.decelerate = function() {
speed -= 5;
};
this.getSpeed = function() {
return speed;
};
};
functional programming
.map() ---> Iterate over array and return new array
var newArray = oldArray.map(function(val) {
return val + 3;
});
.map()iterate array to get array each elementvalcallback function take
valas parameter and add 3 to every value and save results innewArrayinstead of
val, can callback parameter can also includeindexandarraybeing acted on.
.reduce() ---> Iterate over array and condense it into one value
singleVal = array.reduce(function(preVal, curVal){
return preVal + curVal;
}, 0);
.reduce()iterate array to get each elementcurValand keep accumulatorpreVal- second para of
.reduce()is the initial val of accumulator - sum all values of old array and return it.
.filter() ---> Iterate array and filter out elements where don't meet condition
array = array.filter(function(val) {
return val !== 5;
});
.filter()iterate array to get each elementval- callback take
valas para when keep that element when it returns true, otherwise filters it out. - remove array elements that are equal to five and return a new array
.sort() ---> Iterate array and sort it
array.sort(function(a, b) {
return a - b;
});
sort from smallest to largest
.sort()iterate array to get each elementaand the next of itbcallback is a compare function:
- a before b ---> return <0
- a after b ---> return >0
- a equal b ---> return 0
sort alphabetically if no callback
the original array will be altered
.reverse() ---> reverse array
newArray = array.reverse();
- simple as it looks like
- original array will be altered
.concat() ---> Concatenate array
newArray = oldArray.concat(otherArray);
- return new array
.split() ---> split string into array
array = string.split(' ');
- argument is delimiter
.join() ---> join array element into a string
var veggies = ["Celery", "Radish", "Carrot", "Potato"];
var salad = veggies.join(" and ");
console.log(salad); // "Celery and Radish and Carrot and Potato"
- argument is delimiter
.replace() ---> replace a specified value in string
//remove all non-alphanumeric char from a str
str = str.replace(/[^0-9a-z]/gi, '');
1st arg is search value/ regular expression; 2nd arg is replacement value
original string does not change
.substr(start [,length]) ---> extracts parts of a string
var str = "Hello world!";
var res = str.substr(11, 1); // "!"
var res = str.substr(2); // "llo world!"
- original string does not change
.slice(start [,end]) ---> extracts parts of a string/array
var str = "Hello world!";
var res = str.slice(3); // "lo world!"
var res = str.slice(3, 8); // "lo wo"
var res = str.slice(-1); // "!"
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3); // ["Orange", "Lemon"]
select parts from the
startand ends at, but does not include, the givenendargument.new string/array will be returned
.indexOf(item [,start]) ---> returns index of item in string/array
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("e"); // 1
var n = str.indexOf("e", 5); // 14
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple"); //2
var a = fruits.indexOf("Apple", 4); // 6
- return -1 when item no found
charCodeAt() ---> returns the unicode of the character at a specified index in a string
var str = "HELLO WORLD";
str.charCodeAt(0); // 72
- some common unicode
0-9: 48 - 57A-Z: 65 - 90a-z: 97 - 122
String.fromCharCode(n1 [, n2, ..., nX]) ---> converts Unicode values into characters.
var res = String.fromCharCode(72, 69, 76, 76, 79); // "HELLO"
- static method, always be used in form
String.fromCharCode()