This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sayHiLater() { | |
| var greeting = 'Hi'; | |
| // uses first-class function and create a function object on the flay by function expressions | |
| setTimeout(function() { | |
| console.log(greeting); // 30 sec later, it still have access to greeting variable | |
| }, 30000); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function callLater(callback) { | |
| console.log('Some work...'); | |
| callback(); | |
| } | |
| callLater(function() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var person = { | |
| firstname: 'John', | |
| lastname: 'Doe', | |
| getFullName: function() { | |
| var fullname = this.firstname + ' ' + this.lastname; | |
| return fullname; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* No wraping */ | |
| var arr1 = [1, 2, 3]; | |
| var arr2 = []; | |
| var limiter = 2; | |
| arr1.forEach(function(item, i){ | |
| arr2.push(item > 2); | |
| }); | |
| console.log(arr2); // [false, false, true] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a === undefined; // Uncaught ReferenceError: a is not defined | |
| var a; | |
| a === undefined; // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var person = { | |
| firstname: 'Default', | |
| lastname: 'Default', | |
| getFullName: function(){ | |
| return this.firstname + ' ' + this.lastname; | |
| } | |
| } | |
| var john = { | |
| firstname: 'John', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var arr = []; | |
| var obj = {}; | |
| console.log(arr.__proto__.constructor); // ƒ Array() { [native code] } | |
| console.log(object.__proto__.constructor); // ƒ Object() { [native code] } | |
| console.log(arr.__proto__.__proto__.constructor); // ƒ Object() { [native code] } | |
| console.log(object.__proto__.__proto__); // null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Person(firstname, lastname) { | |
| console.log('Invoke function: Person'); | |
| this.firstname = firstname || 'Default'; | |
| this.lastname = lastname || 'Default'; | |
| } | |
| // JavaScript has no classes, use this syntax atract Java users | |
| var john = new Person('John', 'Doe'); | |
| console.log(john); | |
| // Invoke function: Person |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function loadScript(scriptName, callback) { | |
| if (!jsArray[scriptName]) { | |
| var promise = jQuery.Deferred(); | |
| // adding the script tag to the head as suggested before | |
| var body = document.getElementsByTagName('body')[0], | |
| script = document.createElement('script'); | |
| script.type = 'text/javascript'; | |
| script.src = scriptName; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Person(firstname, lastname) { | |
| this.firstname = firstname || 'Default'; | |
| this.lastname = lastname || 'Default'; | |
| } | |
| var john = new Person('John', 'Doe'); | |
| console.log(john.__proto__.constructor === Person); // true | |
| // .prototype | |
| Person.prototype.getFullName = function() { |