JavaScript is one of the most popular skills in today’s job market. It allows you to create both client- and server-side applications quickly and easily. Having a solid understanding of this powerful and versatile language is essential to anyone who uses it.
“Practical JavaScript Programming” does not only focus on best practices, but also introduces the fundamental concepts. This course will take you from JavaScript basics to advanced. You’ll learn about topics like Data Types, Functions, Events, AJAX and more.
4. 4
Wilson Su
Front-end Developer, HIE
● 6 years in web design
● Specialize in JavaScript /
CSS / HTML / OOP / Git
● Familiar with PHP / Design
Pattern
● Interested in UI & Ix Design
wilson_su@trend.com.tw
39. What Is JSON?
39
JSON
● JSON stands for JavaScript Object Notation
● It is derived from JavaScript
● JSON uses the file extension .json
● The official MIME type for JSON is application/json
45. Stringify An Object Using toJSON()
45
1. var car = { brand: 'audi', color: 'BLACK' };
2.
3. car.toJSON = function () {
4. return {
5. brand: car.brand.toUpperCase(),
6. color: car.color.toLowerCase()
7. };
8. };
9.
10. JSON.stringify(car); // {"brand":"AUDI","color":"black"}'
46. Stringify An Object Using Replacer
46
1. var book = {
2. title: 'JavaScript: The Good Parts',
3. price: 20
4. };
5. var replacer = function (key, value) {
6. return key === 'price' ? value * 30 : value;
7. };
8.
9. JSON.stringify(book, replacer);
10. // {"title":"JavaScript: The Good Parts","price":600}
47. Parsing JSON Using Reviver
47
1. var book = '{"title":"JS: The Good Parts","price":600}';
2.
3. JSON.parse(book, function (key, value) {
4. return key === 'price' ? value / 30 : value;
5. });
6. // {title: 'JS: The Good Parts', price: 20}
48. Stringify Objects And Arrays To JSON String
48
1. var pet = {
2. id: 0xff,
3. name: null,
4. type: undefined,
5. eat: function () {}
6. };
7. var misc = [ 0b11, null, undefined, function () {} ];
8.
9. JSON.stringify(pet); // ?
10. JSON.stringify(misc); // ?
1. var pet = {
2. id: 0xff,
3. name: null,
4. type: undefined,
5. eat: function () {}
6. };
7. var misc = [ 0b11, null, undefined, function () {} ];
8.
9. JSON.stringify(pet); // '{"id":255,"name":null}'
10. JSON.stringify(misc); // ?
1. var pet = {
2. id: 0xff,
3. name: null,
4. type: undefined,
5. eat: function () {}
6. };
7. var misc = [ 0b11, null, undefined, function () {} ];
8.
9. JSON.stringify(pet); // '{"id":255,"name":null}'
10. JSON.stringify(misc); // '[3,null,null,null]'
63. An Example Using Logical OR
63
1. /* WARNING: The Logical OR operator returns the value of its
second operand, if the first one is falsy, otherwise the value
of the first operand is returned. */
2. function create (options) {
3. return {
4. type: options.type || 'button'
5. active: options.active || true
6. };
7. }
8.
9. create({ active: false });
10. // ?
1. /* WARNING: The Logical OR operator returns the value of its
second operand, if the first one is falsy, otherwise the value
of the first operand is returned. */
2. function create (options) {
3. return {
4. type: options.type || 'button'
5. active: options.active || true
6. };
7. }
8.
9. create({ active: false });
10. // {type: 'button', active: true}
66. An Example Using Bitwise AND
66
1. var hex = 'ffaadd';
2. var rgb = parseInt(hex, 16); // 16755421
3. var R = rgb >> 16 & 0xff; // 255
4. var G = rgb >> 8 & 0xff; // 170
5. var B = rgb & 0xff; // 221
67. An Example Using Bitwise NOT
67
1. Math.floor(2.5); // 2
2. Math.floor(2.1); // 2
3. Math.floor(-2.5); // -3
4.
5. ~~(2.5); // 2
6. ~~(2.1); // 2
7. ~~(-2.5); // -2
68. An Example Using Bitwise NOT
68
1. var numbers = [100, 200, 300];
2.
3. if (numbers.indexOf(200) > -1) { // Condition is truthy }
4. if (numbers.indexOf(500) === -1) { // Condition is truthy }
5.
6. if (~numbers.indexOf(200)) { // Condition is truthy }
7. if (!~numbers.indexOf(500)) { // Condition is truthy }
74. in Operator - Examples of Strings
74
1. var baseball = new String('Baseball');
2. 'length' in baseball; // true
3. 'ball' in baseball; // false
4.
5. var basketball = 'Basketball';
6. 'length' in basketball; // ?
1. var baseball = new String('Baseball');
2. 'length' in baseball; // true
3. 'ball' in baseball; // false
4.
5. var basketball = 'Basketball';
6. 'length' in basketball; // TypeError
76. JavaScript URIs
76
1. <a href="javascript:void(0);">
2. Click here to do nothing
3. </a>
4.
5. <a href="javascript:;">
6. Click here to do nothing
7. </a>
8.
9. <a href="javascript:void(alert('Hello!'));">
10. Click here to display a alert box
11. </a>
77. The strength of JavaScript is
that you can do anything. The
weakness is that you will.
- REG BRAITHWAITE
77