Skip to content

Instantly share code, notes, and snippets.

@oyilmaztekin
Last active June 24, 2021 15:46
Show Gist options
  • Select an option

  • Save oyilmaztekin/5a394a1cd2a9d250151bb22e02c27533 to your computer and use it in GitHub Desktop.

Select an option

Save oyilmaztekin/5a394a1cd2a9d250151bb22e02c27533 to your computer and use it in GitHub Desktop.
Arrow Fonksiyonu nerelerde Kullanamayız
// CONSTRUCTOR PATTERN
var Message = (text) => {
this.text = text;
};
// Throws "TypeError: Message is not a constructor"
var helloMessage = new Message('Hello World!');
// 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
// 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';
});
// 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
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