Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Private slots are new and can be created via Instance private fields

// ES2022
class InstPrivateClass {
  #privateField1 = 'private field 1'; // (A)
  #privateField2; // (B) required!
  constructor(value) {
    this.#privateField2 = value; // (C)
  }
  /**
   * Private fields are not accessible outside the class body.
   */
  checkPrivateValues() {
  console.log(this.#privateField1); // output -> 'private field 1'
  console.log(this.#privateField2); // output -> 'constructor argument'

  }
}

const inst = new InstPrivateClass('constructor argument');
  inst.checkPrivateValues();


console.log("inst", Object.keys(inst).length === 0) //output -> inst, true
Comment

Private slots are new and can be created via Instance and static private fields

// ES2022
class InstPrivateClass {
  #privateField1 = 'private field 1'; // (A)
  #privateField2; // (B) required!
  static #staticPrivateField = 'hello';
  constructor(value) {
    this.#privateField2 = value; // (C)
  }
  /**
   * Private fields are not accessible outside the class body.
   */
  checkPrivateValues() {
    console.log(this.#privateField1); // output -> 'private field 1'
    console.log(this.#privateField2); // output -> 'constructor argument'

  }

  static #twice() {
    return this.#staticPrivateField + " " + this.#staticPrivateField;
  }

  static getResultTwice() {
    return this.#twice()
  }
}

const inst = new InstPrivateClass('constructor argument');
inst.checkPrivateValues();


console.log("inst", Object.keys(inst).length === 0) //output -> "inst", true
console.log(InstPrivateClass.getResultTwice()); // output -> "hello hello"
Comment

Private slots are new and can be created via Private methods and accessors

// ES2022
class MyClass {
  #privateMethod() {}
  static check() {
    const inst = new MyClass();

    console.log(#privateMethod in inst) // output-> true

    console.log(#privateMethod in MyClass.prototype) // output-> false

    console.log(#privateMethod in MyClass) // output-> false
  }
}
MyClass.check();
Comment

Private slots are new and can be created via Static initialisation blocks in classes

class Translator {
  static translations = {
    yes: 'ja',
    no: 'nein',
    maybe: 'vielleicht',
  };
  static englishWords = [];
  static germanWords = [];
  static { // (A)
    for (const [english, german] of Object.entries(this.translations)) {
      this.englishWords.push(english);
      this.germanWords.push(german);
    }
  }
}


console.log(Translator.englishWords, Translator.germanWords)
//Output -> ["yes", "no", "maybe"], ["ja", "nein", "vielleicht"]
Comment

Private slots are new and can be created via Private slot checks

class C1 {
  #priv() {}
  static check(obj) {
    return #priv in obj;
  }
}

console.log(C1.check(new C1())) // output true
Comment

PREVIOUS NEXT
Code Example
Javascript :: indexable values js 
Javascript :: How to change color of an icon, text or other component with ReactNative useState Hook 
Javascript :: Get physical path in javascript 
Javascript :: how to get 4 columns with masonryjs 
Javascript :: Focus next input once reaching maxlength value 
Javascript :: action creators in redux 
Javascript :: typeorm with better sqlite Loading from the database 
Javascript :: HimalayanCoffeeHouse Noida 
Javascript :: how to use sort with tiebreak in js 
Javascript :: How To Start Any Program In Background Using Vbscript 
Javascript :: setinterval clearinterval querySelector until render 
Javascript :: how to add heaeader to http angular client 
Javascript :: SuiteScript https.post a pdf file 
Javascript :: Automatically Refresh or Reload a Page using http-equiv 
Javascript :: adding values for metaboxes in wordpress 
Javascript :: copy file using java script 
Javascript :: javascript array keyshort 
Javascript :: merge json data in main.go in golang 
Javascript :: maxscript saveMaxFile 
Javascript :: tailwind intenseness react 
Javascript :: programmatically change mongoose schema enum values 
Javascript :: group by hash in jquery 
Javascript :: render(<App /); const linkElement = screen.getByText(/learn react/i); expect(linkElement).toBeInTheDocument(); 
Javascript :: mouselight js 
Javascript :: javascript replace char if not present another character 
Javascript :: gsheet calculate next tuesday date 
Javascript :: github react hardhat nft marketplace application 
Javascript :: how to run the counter when we reach at a specific section in jquery 
Javascript :: for of exemple 
Javascript :: get json model from another component in sapui5 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =