Last update: 2016/4/4
// Javascript code blocks // // Author: ch3cooh // // !!! BUG ALERT !!! // To oschina.net: // A single quote inside line comment will cause the syntax highlighting to fail :( // print a split line on the screen function title_line(title) { var len = 78; title = title || ''; var head_len = Math.floor((len - title.length)/2); var s = ''; for(var i=0; i<head_len; i++) { s += '-'; } s += title; for(var i=0; i<len-head_len-title.length; i++) { s += '-'; } console.log(s); } // dollar sign can be used in variable names in javascript (function (){ title_line('use $ as a variable name') var $ = 'money'; var $$ = 'more money'; var $$$; console.log($); console.log($$); console.log($$$); })(); // one way to make an array in javascript (function () { title_line('define an array in js'); var list = new Array(10); list[2] = 'bonjour'; console.log('length of the list is', list.length); console.log('content of this list:'); for(var i=0; i<list.length; i++) { console.log("list[", i, "] = ", list[i]); } })(); // Question: Is there for-each in javascript? (function () { title_line('another way to define an array'); var list = ['un', 'deux', 'trois', 'quatre', 'cinq']; for(var i=0; i<list.length; i++) { console.log(list[i]); } })(); // Redeclaration of a variable does not change its original value (function() { title_line('declare a variable twice'); var old = 'long time ago'; console.log(old); var old; console.log(old); })(); // Declare a variable using new (function() { title_line('declare with new'); var num = new Number; // here hum is initialized! console.log(typeof num, '---', num); num = "je ne suis pa un nombre"; console.log(typeof num, '---', num); })(); // build an object in javascript (function() { title_line('object == dictionary?'); var study_master = { 'name':'gpa_guru', 'gpa':4.3, 'study' : function () { this.gpa+=0.1; }, 'bluff' : function () { console.log('My gpa is as low as', Math.min(this.gpa-1.0, this.gpa)); }, 'talk' : function () { console.log('Bonjour! je suis', this.name, '!') } }; study_master.talk(); study_master.bluff(); study_master.study(); study_master.bluff(); console.log(typeof study_master); })(); // try-catch-throw (function() { title_line('try-catch-throw'); try { try { foo(); } catch(err) { console.log(typeof err, '---', err); throw 'not in a good mood today'; } } catch(err) { console.log(typeof err, '---', err); } })(); // define an object (function() { title_line('several ways to define an object'); var o1 = {'233':'lol'}; console.log(o1['233']); console.log(o1[233]); console.log(o1[666]); // undefined var o2 = {name: 'Richard Feynmann'}; console.log(o2.name); // 2 ways to access the properties of an object: // a. bracket notation: obj['name'] // b. dot notation: obj.name })(); // null and undefined (function() { title_line('null and undefined'); var nothing; console.log(typeof nothing, '---', nothing); var nothing = null; console.log(typeof nothing, '---', nothing); var nothing = {}; console.log(typeof nothing, '---', nothing); })(); // create an object using function (function() { title_line('another way to define an object'); function Student(name, age, gender, major) { this.name = name || 'unknown'; this.age = age || 'unknown'; this.gender = gender || 'secret'; this.major = major || 'no decided yet'; this.sayhi = function () { console.log('Bonjour! Je suis', this.name,'!'); } } var tom = new Student('tom'); tom.sayhi(); })(); // what is in this (function() { title_line('this variable'); var property_count = 0; var method_count = 0; console.log('These are the properties and methods of this:'); for(var p in this) { console.log(p); if(typeof this[p] == 'function') { method_count++; } else { property_count++; } } console.log(method_count + " methods and " + property_count + ' properties in total'); })(); // a helper function to display all the fields and methods of a object function inspect(obj) { var info = ''; for(var p in obj) { info += '[' + typeof obj[p] + '] ' + p + ' : ' + obj[p] + '\n'; } return info; } (function() { title_line('object again'); function Person(name) { this.name = name; } var tom = new Person('tom'); console.log(typeof tom); console.log(inspect(tom)); // strangely 'constructor' is not displayed console.log(tom.constructor); })(); (function () { title_line('instanceof operator'); function quick_eval(str) { console.log (str + ' --> ' + eval(str)); } quick_eval('233 instanceof Object'); quick_eval('console.log instanceof Function'); quick_eval('"bonjour" instanceof Object'); quick_eval('666 instanceof Number'); quick_eval('new Number(3.14) instanceof Number'); quick_eval('new String("naive") instanceof Object'); quick_eval('String(233) instanceof Object'); // typeof String(233) ---> 'string' // typeof new String(233) ---> 'object' })(); (function() { title_line('strange behavior of constructor'); var o1 = new (function() { // var this;``-- the compiler is doing such thing this.name = 'some stupid object'; // return this; })(); console.log(typeof o1); console.log(o1.constructor); console.log(o1.name); var o2 = new (function() { this.name = 'another stupid object'; return { 'name': 'not that stupid' }; })(); console.log(typeof o2); console.log(o2.name); console.log(o2.constructor); var o3 = new (function() { this.name = 'still an stupid object'; return 233; })(); console.log(o3.name); })(); // built-in objects in javascript // Data wrappers: Object, Array, Function, Boolean, Number, String // Utility objects: Math, Date, RegExp // Error objects: Error // Object // o.constrcutor // o.toString() // o.valueOf() // Array objects have a length while ordinary objects do not (function() { title_line('array has length'); var o1 = []; var o2 = {}; console.log(typeof o1, o1.length); console.log(typeof o2, o2.length); // os.length is undefined })(); // change the size of an array via setting its size (function() { title_line('change array size'); var o = [1,2,3,4,5]; console.log(o); o.length = 10; console.log(o); console.log('o[7] = ', o[7]); // undefined })(); // other array methods (function() { title_line('more array operations'); var o = [1,2,3,4,5,6,7,8,9]; console.log(o.join('-o-')); // elements of o is automatically converted to string // the two arguments of slice specify the start and end point (not included) console.log(o.slice(1,2)); // [1, 2) // the second argument of splice specifies the number of elements to remove var oo = o.splice(2,3, 'deux', 'trois'); console.log('oo=', oo, 'o=', o); })(); // the constructor of a function (function() { title_line('the constructor of a function'); var sum = Function('a', 'b', 'return a + b;'); console.log(sum(1,4)); })();