Last active
August 29, 2015 14:21
-
-
Save piyasde/b43db1e0bd50cd0c22d3 to your computer and use it in GitHub Desktop.
es6 Fat Arrow function in Server Side
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 a = [ | |
| "Hydrogen", | |
| "Helium", | |
| "Lithium", | |
| "Beryllium" | |
| ]; | |
| var a2 = function(s){ return s.length }; | |
| var a3 = ((s) => s.length); | |
| console.log(a2(a)); | |
| console.log(a3(a)); | |
| // Simple => | |
| var str = "Please find where there is a find"; | |
| var searchStr = "find"; | |
| var pos = function(totalStr,stringToSearch){ return str.indexOf(stringToSearch) }; | |
| console.log(pos(str,searchStr)); | |
| var es6pos = (totalStr,stringToSearch) => str.indexOf(stringToSearch); | |
| console.log(es6pos(str,searchStr)); | |
| // another example => | |
| var str = "Please find where there is a find"; | |
| var searchStr = "find"; | |
| var pos = function(totalStr,stringToSearch){ return str.indexOf(stringToSearch) }; | |
| var logIt = function(anything){console.log( anything)}; | |
| logIt(pos(str,searchStr)); | |
| var intermediateResult = ((totalStr,stringToSearch) => str.indexOf(stringToSearch)) ; | |
| var es6Logit = intermediateResult => console.log(intermediateResult); | |
| es6Logit(intermediateResult(str,searchStr)); | |
| // foreach i.e in statement body | |
| //ecma5 | |
| var arr = ["a", "b", "c"]; | |
| arr.forEach(function(entry) { | |
| console.log(entry); | |
| }); | |
| //or the old way | |
| var index; | |
| for (index = 0; index < arr.length; ++index) { | |
| console.log(arr[index]); | |
| } | |
| //now in es6 | |
| // Statement bodies | |
| arr.forEach(elem => console.log(elem)); | |
| // Lexical this | |
| //ecma5 | |
| function Person() { | |
| var self = this; | |
| self.age = 14; | |
| self.grow = function () { | |
| // The callback refers to the `self` variable of which | |
| // the value is the expected object. | |
| return self.age++; | |
| }; | |
| } | |
| var p = new Person(); | |
| console.log(p.grow()); | |
| console.log(p.grow()); | |
| //ecma6 | |
| function Person(){ | |
| this.age = 14; | |
| this.grow = () => { | |
| return this.age++; // |this| properly refers to the person object | |
| }; | |
| } | |
| var p = new Person(); | |
| console.log("es6 --"+ p.grow()); | |
| console.log("es6 --"+ p.grow()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment