Skip to content

Instantly share code, notes, and snippets.

View cihat's full-sized avatar
💻
writing code🚀

Cihat SALİK cihat

💻
writing code🚀
View GitHub Profile
@cihat
cihat / branch conflict
Created April 30, 2021 10:12
branch conflict
git add -A
git stash
git checkout master
git pull origin master
git checkout development(branch)
@cihat
cihat / Making AJAX calls in pure JavaScript
Created February 1, 2021 12:33
Making AJAX calls in pure JavaScript
const getCountryData = country => {
const request = new XMLHttpRequest();
request.open('GET', `https://restcountries.eu/rest/v2/name/${country}`);
request.send();
request.addEventListener('load', function () {
const [data] = JSON.parse(this.responseText);
console.log(data);
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
@cihat
cihat / essential-javascript-links.md
Created December 6, 2020 00:10 — forked from ericelliott/essential-javascript-links.md
Essential JavaScript Links
Types of Properties
There are two different types of properties: data properties and accessor
properties. Data properties contain a value, like the name property from earlier examples in this chapter.
The default behavior of the [[Put]] method
is to create a data property, and every example up to this point in the
chapter has used data properties. Accessor properties don’t contain a value
but instead define a function to call when the property is read (called
a getter), and a function to call when the property is written to (called a
setter). Accessor properties only require either a getter or a setter, though
they can have both.
Removing Properties
Just as properties can be added to objects at any time, they can also be
removed. Simply setting a property to null doesn’t actually remove the
property completely from the object, though. Such an operation calls
[[Set]] with a value of null, which, as you saw earlier in the chapter, only
replaces the value of the property. You need to use the delete operator to
completely remove a property from an object.
The delete operator works on a single object property and calls an
internal operation named [[Delete]]. You can think of this operation as
removing a key/value pair from a hash table. When the delete operator is
In most cases, the in operator is the best way to determine whether
the property exists in an object. It has the added benefit of not evaluating the value of the property, which can be important if such an evaluation is likely to cause a performance issue or an error.
In some cases, however, you might want to check for the existence of
a property only if it is an own property. The in operator checks for both
own properties and prototype properties, so you’ll need to take a different
approach. Enter the hasOwnProperty() method, which is present on all objects
and returns true only if the given property exists and is an own property.
For example, the following code compares the results of using in versus
hasOwnProperty() on different properties in person1:
Detecting Properties
Because properties can be added at any time, it’s sometimes necessary to
check whether a property exists in the object. New JavaScript developers
often incorrectly use patterns like the following to detect whether a property exists:
// unreliable
if (person1.age) {
// do something with age
}
The bind() Method
The third function method for changing this is bind(). This method was
added in ECMAScript 5, and it behaves quite differently than the other
two. The first argument to bind() is the this value for the new function.
All other arguments represent named parameters that should be permanently set in the new function. You can still pass in any parameters that
aren’t permanently set later.
The following code shows two examples that use bind(). You create
the sayNameForPerson1() function by binding the this value to person1, while
sayNameForPerson2() binds this to person2 and binds the first parameter as
"person2".
The apply() Method
The second function method you can use to manipulate this is apply(). The
apply() method works exactly the same as call() except that it accepts only
two parameters: the value for this and an array or array-like object of
parameters to pass to the function (that means you can use an arguments
object as the second parameter). So, instead of individually naming each
parameter using call(), you can easily pass arrays to apply() as the second
argument. Otherwise, call() and apply() behave identically. This example
shows the apply() method in action:
@cihat
cihat / gist:e833d3ed62fa08be99b12d09fedd3e53
Created November 16, 2020 09:20
call() method in JavaScript
The call() Method
The first function method for manipulating this is call(), which executes
the function with a particular this value and with specific parameters.
The first parameter of call() is the value to which this should be equal
when the function is executed. All subsequent parameters are the parameters that should be passed into the function. For example, suppose you
update sayNameForAll() to take a parameter:
function sayNameForAll(label) {
console.log(label + ":" + this.name);
}