Dev Notes
  • Giriş
  • nodejs
    • middleware
    • executable
    • cluster module
  • javascript
    • promise
    • async/await
    • array methods
    • static & private
  • Golang
    • struct
  • Elasticsearch
    • elasticsearch nedir
    • multiple search
  • Design Patterns
    • proxy pattern
    • provider pattern
    • prototype pattern
    • container/presentational pattern
    • observer pattern
    • module pattern
  • nuxtjs
    • routing
    • nuxt-child
  • swift
    • if & guard
    • optional & non-optional
    • array & dictionary & set
    • tuples
    • class & access modifiers
    • struct & mutating func
    • protocol
    • enum
    • subscript
  • other
    • neo4j
    • cache strategies
    • mysql full text search
  • References
    • nodejs
    • javascript
    • swift
Powered by GitBook
On this page
  • class field declarations
  • static
  • private
Edit on GitHub
  1. javascript

static & private

class field declarations

ES2022 ile bir constructor ihtiyaç olmadan class genelinde özellik ataması yapabiliriz.

//OLD
class Member {
    constructor() {
        this.name = 'Bahri'
    }
}

//NEW
class Member {
    name = 'Bahri';
}

static

ES2022 ile class içerisinde static özellikler tanımlayabiliriz. Bu static özelliklere instance oluşturmadan erişim sağlayabiliriz.

class Member {
  static name = "Bahri";

  static save() {
    console.log(`${this.name} has been recorded`)
  }
}

Member.save() //Bahri has been recorded

private

ES2022 ile private özellik tanımlayabiliriz. Özelliklerin başına # ekleyerek private olarak tanımlama yapabiliriz. Bu özelliklere class dışarısından erişim sağlanamaz. Sadece getter ve setter tanımlamaları yapılarak erişim sağlayabiliriz.

class Member {
  #name = "Bahri";

  get name() {
    return this.#name
  }

  set name(name) {
    this.#name = name
  }
}

const member = new Member();
member.name = 'Kemal'

console.log(member.name) //Kemal

static private özellikler de tanımlanabiliriz. Bu özelliklere erişmek için getter ve setter tanımlamaları yapılmalıdır.

class Member {
  static #name = "Bahri";

  static get name() {
    return this.#name
  }
}

console.log(Member.name) //Bahri
Previousarray methodsNextstruct

Last updated 2 years ago