Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 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

PREVIOUS NEXT
Code Example
Javascript :: json serializable snake case 
Javascript :: AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal: 
Javascript :: route methods 
Javascript :: angular select option default value ngfor 
Javascript :: min expression postgresql 
Javascript :: disabling first item in dropdownlist 
Javascript :: typeorm with better sqlite using query builder 
Javascript :: crear un texto dinamicamente con javascript 
Javascript :: change teh value of a slider p5js 
Javascript :: get output dir from webpack options 
Javascript :: id always returing null angular 
Javascript :: how to print 1 to n numbers without using loop javascript 
Javascript :: if conprimido js 
Javascript :: Example of String.prototype.replaceAll in es12 
Javascript :: Array helper functions in ES6 
Javascript :: ngFor fake 
Javascript :: Imports should be sorted alphabetically sort-imports 
Javascript :: set value 
Javascript :: angular cache interceptor 
Javascript :: for loop display numbers 1 to 10 javascript 
Javascript :: react regions 
Javascript :: json mapper 
Javascript :: how to add edit and delete rows of a html table with javascript 
Javascript :: amcharts 3d column chart export 
Javascript :: get call log in react native with filter android 
Javascript :: mutiple if in express handlebars 
Javascript :: getIdfrommodelOnClickAjax 
Javascript :: How to determine dropdown should show upward or downward direction 
Javascript :: remove object id from the specific id 
Javascript :: url(image loacation) give a class 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =