Image for post
Image for post
Photo by twinsfisch on Unsplash

JAVASCRIPT’IN TARIHÇESI

Javascript Spread Kavramı

Javascript Spread bize ne gibi avantajlar sağlar ? Kodun okunmasını nasıl kolaylaştırır ?

Onur Dayıbaşı
Nov 25, 2019 · 3 min read

Javascript Spread Nedir?

...[2,4,6,8] => 2,4,6,8 olmasıdır.

Function Call/Apply Örneği

function sum(x,y,w,z){
let toplam=x+y+w+z; return toplam;
}
console.log(sum.call(this,3,5,7,9)); //24
console.log(sum.apply(this,[3,5,7,9])); /24
console.log(sum.call(this,...[3,5,7,9])); 

Array’ de Kullanım Örnekleri

Array.push()

const arr=[2,3]; arr.push(4,5) console.log(arr);//[2,3,4,5]
const arr=[2,3]; arr.push.apply([4,5]) console.log(arr);
const arr=[2,3]; arr.push([...4,5]) console.log(arr);

Array.splice() Örneği

const arr=[1,2]; arr.splice(1,0,4,5); console.log(arr)//[1,4,5,3]
const arr=[1,...[4,5],2]; console.log(arr)

Array.concat() Örneği

const arr=[1,2]; console.log(arr.concat([3,4],[5])) //[1,2,3,4,5]
console.log([...[1,2],...[3,4],...[5]]);

Array ve Object Kopyalama Örneği

const arr1=[1,2]; const arr2=[...arr1]; arr1.push(3);
console.log(arr2);
const obj1={a:1,b:2}; const obj2={...obj1}; obj1['c']=3;
console.log(obj2);

Nesneyi Kopyalama Yöntemleri(Shallow/Deep Copy)

let obj = { a: [1, 2, {users: [{ name: ‘onur’, age: 12 }] }], b:2 };
const cloneObj={…obj}; //same => Object.assign({}, obj);
obj.a[2].users[0].name=’deniz’;
obj.b=4;
console.log(cloneObj);//Result interesting..
//cloneObj.b is 2 not 4 because it clones first level and cloneObj holds a new memory location for b
//but cloneObj.a[2].users[0].name is 'deniz' why because it hold reference cloning is not deeply.
const clonedeep = require('lodash.clonedeep')
function clone(obj) {
if (obj === null || typeof (obj) !== 'object' || 'isActiveClone' in obj)
return obj;
if (obj instanceof Date)
var temp = new obj.constructor(); //or new Date(obj);
else
var temp = obj.constructor();
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
obj['isActiveClone'] = null;
temp[key] = clone(obj[key]);
delete obj['isActiveClone'];
}
}
return temp;
}

Math’ da Kullanım Örnekleri

console.log(Math.max(1,5,3)); //5
console.log(Math.max.apply(this,[1,5,3])); //5
console.log(Math.max(…[1,5,3])); //5

Frontend Development With JS

Frontend Development With Javascript

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store