1. JavaScript Objects
Objects are variables too, but objects can contain many values.
1
var car = {type:"flat", model:"500",color:"white"}
1.1 The value are written as name:value pairs
1.2 JavaScript objects are container for named value.Two ways to access objects:
2.1.
1
objectName.propertyName
2.2.
1
objectName["propertyName"]
2. JavaScript Scope
In JavaScript, scope is the set of variables,objects,and functions you have access to.
Local JavaScript Variables
- Variables declared within a JavaScript function, become LOCAL to the function.
- Local variable have local scope: They can be accessed within the function.
- Local variables are created when a function starts, and deleted when the function is completed.
Global JavaScript Variables
- Variables declared outside a function, becomes Global.
- A global variable has global scope: All scripts and functions on a web page can access it.
Automatically global
- If you assign a value to a variable that has not been declared, it will automatically become a GOLBAL variable.
3. JavaScript String methods
Finding a String in a String
The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a String:
1
2var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");The lastIndexOf() method returns the index of the last occurrence of a specified text in a string.
Searching for a String in a String
- The search() method searches a string for a specifed value and returns the position of the match.
Extracting String parts
- slice(start,end)
- substring(start,end) can’t accept negative indexes
- substr(start,length)
Replacing String Content
- replace() method replaces a specified value with another value in a string.
By default, the replace()function replaces only the first match. To replace all matches, use a regular expression with a g flag(for global match).
1
2str = "Please visit Apple!";
var n = str.replace(/Apple/g,"Microsoft!");The replace() method does not change the string it is called on, it returns a new string.
Converting to Upper and Lower Case
- toUpperCase()
- toLowerCase()
concat() method
- concat() joins two or more strings:
1
2
3var text1 = "Hello!";
var text2 = "World!";
text3 = text1.concat("",text2);
- concat() joins two or more strings:
Extracting String Characters
- There are 2 safe methods for extracting string characters:
- charAt(position) returns the character at a specified index in a string
- charCodeAt(position) returns the unicode of the character at a specified index in a string.
- There are 2 safe methods for extracting string characters:
Converting a String to a Array
- A string can be converted to an array with the split() method:
1
2
3
4var text = "a,b,c,d,e";
text.split(","); // Split on commas
text.split(" "); // Split on space
text.split("|"); // Split on pipe
- A string can be converted to an array with the split() method:
4. JavaScript Array Methods
- Converting Arrays to Strings
- toString() converts an array to a string of(comma separated) array values.
1
2var fruits = ["banana", "orange", "Apple", "Mango"];
fruits.toString();
- toString() converts an array to a string of(comma separated) array values.
- join() method also joins all array elements into a string.
1
2var fruits = ["banana", "orange", "apple", "Mango"];
fruits.join("*");
Popping and pushing
- pop() method removes the last element from an array
- push() method adds a new element to an array(at the end)
Shifting Elements
- shift() is equivalent to popping, working on the first element instead of the last.
- unshift() methods adds a new element to an arry(at the beginning), and “unshifts” older elements.
Changing Elements
1
2
3
4
5
6
7
8
9
10
11var fruits = ["banana", "orange", "apple", "Mango"];
fruits[fruits.length] = "kiwi";
```
5. Deleting Elements
* Using Delete may leave undefine holes in the array. Use pop() or shift() instead.
``` JavaScript
var fruits = ["banana", "orange", "apple", "Mango"];
delete fruits[0]; // Changes the first element in the fruites to undefined.
Splicing an Array
- The splice() method can be used to add new items to an array:
1
2var fruits = ["banana", "orange", "apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi");- The first parameter (2) defines the position where new elements should be added(spliced in).
- The second parameter (0) defines how many elements should be removed.
- The rest of the parameter(“Lemon”,”Kiwi”) define the new elements to be added.
Joining Arrays
- concat() method creates a new array by concatenating two arrays.
- Slicing an Array
- slice() method slices out a piece of an array into a new array.
- The slice() method creates a new array. It does not remove any elements from the source array.
5. JavaScript Sorting Arrays
Sorting an Array
- sort() method sorts an array alphabetically:
1
2var fruits = ["Banana","Apple","Orange","Mango"];
fruits.sort();
- sort() method sorts an array alphabetically:
Reversing an Array
- reverse method reverses the elements in an array.
Numeric Sort
- The compare function
- The purpose of the compare function is to define an alternative sort order.
- The compare function should return a negative, zero, or positive value, depending on the arguments:
1
function(a,b) {return a-b}
- The compare function