Last active
June 24, 2021 15:46
-
-
Save oyilmaztekin/5a394a1cd2a9d250151bb22e02c27533 to your computer and use it in GitHub Desktop.
Arrow Fonksiyonu nerelerde Kullanamayız
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
| // CONSTRUCTOR PATTERN | |
| var Message = (text) => { | |
| this.text = text; | |
| }; | |
| // Throws "TypeError: Message is not a constructor" | |
| var helloMessage = new Message('Hello World!'); |
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
| // lambda tarzı işlemleri yazarken fonksiyonu daha da kısaltmak için. | |
| // Dikkat: sadeleşirken okunması ve anlaşılması zorlaşabilir. | |
| let multiply = (a, b) => b === undefined ? b => a * b : a * b; | |
| let double = multiply(2); | |
| double(3); // => 6 | |
| multiply(2, 3); // => 6 | |
| //ES5 Versiyon | |
| function multiply(a, b) { | |
| if (b === undefined) { | |
| return function(b) { | |
| return a * b; | |
| } | |
| } | |
| return a * b; | |
| } | |
| let double = multiply(2); | |
| double(3); // => 6 | |
| multiply(2, 3); // => 6 |
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
| // dinamik olarak context in değiştiği işlemlerde. | |
| // Örneğin 10 tane div var aynı ID'de, hangisine tıklarsak içeriğini değiştirmek istiyoruz. | |
| var div_div = document.getElementById('myDIV'); | |
| div_div.addEventListener('click', () => { | |
| console.log(this === window); // => true | |
| this.innerHTML = 'Clicked button'; | |
| }); |
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
| // OBJE METODU OLARAK TANIMLAYAMAYIZ | |
| var calculate = { | |
| array: [1, 2, 3], | |
| sum: () => { | |
| console.log(this === window); // => true | |
| return this.array.reduce((result, item) => result + item); | |
| }, | |
| sum2(){ | |
| console.log(this === window); // => false direk objeyi referans alır. | |
| return this.array.reduce((result, item) => result + item); | |
| } | |
| }; | |
| console.log(this === window); // => true | |
| calculate.sum(); // => Throws "TypeError: Cannot read property 'reduce' of undefined" | |
| calculate.sum2(); // => 6 |
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 Orc(name) { | |
| this.orcName = name; | |
| } | |
| Orc.prototype.sayOrcName = () => { | |
| console.log(this === window); // => true | |
| return this.orcName; | |
| }; | |
| var orcish = new Orc('WOAAAAAA!'); | |
| orcish.sayOrcName(); // => undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment