28 Mart 2021 Pazar

NaN

includes metodu
Açıklaması şöyle. Burada NaN kullanımı diğer programlama dillerinden farklılaşıyor. Sadece bit eşitliğine bakılıyor. IEEE 754 - Nan - Matematiksel Olarak Hesaplanamayan Bir Durumda Ortaya Çıkar yazısına bakabilirsiniz. Normalde aritmetik bir işlemin sağ veya sol tarafında NaN varsa sonucun da NaN çıkması gerekir
Same-value-zero equality similar to same-value equality, but +0 and −0 are considered equal.
Örnek
Şöyle yaparız
> [NaN].includes(NaN)
true
Örnek
Şöyle yaparız
const x = NaN, y = NaN;
console.log(x == y); // false                -> using ‘loose’ equality
console.log(x === y); // false               -> using ‘strict’ equality
console.log([x].indexOf(y)); // -1 (false)   -> using ‘strict’ equality
console.log(Object.is(x, y)); // true        -> using ‘Same-value’ equality
console.log([x].includes(y)); // true        -> using ‘Same-value-zero’ equality

15 Şubat 2021 Pazartesi

WebSocket Sınıfı

constructor
Şöyle yaparız
// Create a new WebSocket
let socketConnection = new WebSocket('ws://websocket.mysite.com');
constructor - subprotocol
Şöyle yaparız
// Create a new WebSocket with subprotocols
let socketConnection = new WebSocket('ws://websocket.mysite.com', ['soap', 'xmpp']);
onX metodu
Şöyle yaparız
// When the connection is open, some data is sent to the server
socketConnection.onopen = function () {
  connection.send('Hello, the socket connection is open!'); // Send a message to the server
};

// Log errors
socketConnection.onerror = function (error) {
  console.log('WebSocket Error ' + error);
};

// Log messages from the server
socketConnection.onmessage = function (e) {
  console.log('Server: ' + e.data);
};


22 Aralık 2020 Salı

Hoisting

Giriş
Hoisting kaldırma/yükseltme anlamına gelir. Javascript diline mahsus bir kavram, C++,Java gibi dillerde bulunmaz. Kısaca değişkenlerin ve metodların kodda bulunduğu yerden en üst satıra Javascript engine tarafından taşınması gibi düşünülebilir.

Bir açıklama şöyle.
... variables and functions in JavaScript are hoisted or moved to the top by the JavaScript engine, as if they were moved physically, up to the top so that they work no matter where you put them.
Örnek
Şöyle yaparız
console.log(x);  // undefined
var x = 5;

30 Kasım 2020 Pazartesi

tsconfig.json Dosyası - TypeScript Derleyicisi Tarafından Kullanılır

Giriş
compilerOptions, include, exclude bölümlerinden oluşur

compilerOptions Bölümü
noImplicitReturns, noImplicitThis, noImplicitAny gibi seçenekler kullanılabilir.

Örnek - target
Şöyle yaparız.
{
"compilerOptions": {

  "target": "es5",

  "module": "commonjs",

  "outDir": "./build",

  "strict": true,

  "esModuleInterop": true

  }
}
Örnek - lib
Şöyle yaparız
{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}
Örnek
Şöyle yaparız
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "noImplicitAny": false,
    "experimentalDecorators": true
  },
  "include": ["src", "husky.config.js", "lint-staged.config.js"]
}

16 Kasım 2020 Pazartesi

TypeScript tsc komutu

Giriş
Eğer seçenek vermezsek src olarak tanımlanan dosyaları is olarak derler. @type dosyalarının halen package.json dosyasında tanımlı olması gerekir

--init seçeneği
tsconfig.json dosyası oluşturur

10 Kasım 2020 Salı

class Anahtar Kelimesi

Örnek
Şöyle yaparız
class class1{
  constructor(let x , let y){
     this.x = x;
     this.y = y;
  }
}

class class2 extends class1{

 // x & y just arguments for explaining
 constructor(let x , let y){

   // calling class1 after extends
   super(x , y);
 }
}

Typescript Partial

Giriş
Belirtilen tipin tüm alanlarını optional yapar. Birim testi için kullanılabilir

Örnek
Şöyle yaparız
export interface Employee {
   id: string;
   name: string;
   department: Department;
   position: Position;
}
export function buildEmployee(
  {
    id = "abc123",
    name = "Jim",
    department = buildDepartment(),
    position = buildPosition(),
   }: Partial<Employee> = {}
): Employee {
  return {
     id,
     name,
     department,
     position,
  }
}
const dummyEmployee = buildEmployee();
Örnek
Şöyle yaparız
interface User {
  id: int;
  email: string;
  password: string;
  hairColor?: string;
}

function update(user: Partial<User>) {...}