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
| //All this following function expression will | |
| //throw the refrence error, which will raise | |
| //when de-referencing an invalid reference | |
| (function foo() { | |
| console.log('hello world'); | |
| })(); | |
| //foo(); | |
| !function foo() { | |
| console.log('beep boop'); |
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
| //remove array element function | |
| //JavaScript 1.8 | |
| function RemoveElement(array, element) { | |
| !!let (pos = array.lastIndexof(element)) pos != -1 && array.slice(pos, 1); | |
| } | |
| var array = [1, 2, 3, 4, 5]; | |
| var pos = array.indexOf(2); | |
| pos > -1 && array.slice(pos, 1); |
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
| public final class Outer { | |
| //The anonymous inner class | |
| Object anonymous = new Object() {}; | |
| { | |
| class Local{} | |
| Local l = new Local(); | |
| } | |
| Outer() { |
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
| //The following cases occur during the compile time | |
| public final class Cr { | |
| //Constant folding | |
| static final int num1 = 1; | |
| static final int num2 = 2; | |
| static int num3 = 3; | |
| static int num4 = 4; | |
| //Method overload | |
| void func(String s) { |
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
| concurrent connections and connection per second are two different beasts. | |
| To handle 1m concurrent connections a traditional synchronous server would | |
| create 1m threads (or worst, processes) with all the stacks and memory needed. | |
| Node handles that in a sngle thread, but the more connections there are, the | |
| bigger latency per request is need to handle them. because it's single-threaded, | |
| you can however utilize the cluster module to use more than one cpu core on your | |
| machine. | |
| But to handle 1m connections per second, this is about latency, and this you | |
| can only get for the price of higher resource usage. cluster module, horizontal |
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
| //Get the object property descriptor | |
| var obj = {beep: 'boop'}; | |
| console.log(Object.getOwnPropertyDescriptor(obj, 'beep')); | |
| //Modify the objct property descriptor | |
| var obj = {beep: 'boop'}; | |
| Object.defineProperty(obj, 'beep', { | |
| value: 'beep', | |
| writable: false, | |
| enumerable: false, |
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
| public final class Art { | |
| private static void checkRange(String s1, String s2) { | |
| if(s1 == null || s2 == null) | |
| throw new NullPointerException("null"); | |
| if(s1.length() == 0 || s2.length() == 0) | |
| throw new IllegalArgumentException("zero"); | |
| if(s1.length < s2.length()) | |
| throw new IllegalArgumentException("length error"); | |
| } |
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
| //Master.js | |
| var cp = require('child_process'); | |
| var fork = require('child_process').fork; | |
| var spawn = require('child_process').spawn; | |
| var cpus = require('os').cpus(); | |
| //Use fork to crate new process | |
| for(var i = 0; i < cups.length; i++) { | |
| fork('worker.js'); | |
| } |
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
| 1.Get the max and min number in the array of numbers | |
| var arr = [1, 2, 3, 4, 5]; | |
| var max = Math.max.apply(Math, arr); | |
| var min = Math.min.apply(Math, arr); | |
| 2.Empty the array | |
| var arr = [1, 2, 3]; | |
| arr.length = 0; | |
| 3.Don't use delete to move an item in the array |
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 stream = require('stream'); | |
| var s = new Stream; | |
| s.readable = true; | |
| VS: | |
| var stream = require('stream'); | |
| var Readable = stream.Readable; |