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

PREVIOUS NEXT
Code Example
Javascript :: javascriot html eqaul to jquery 
Javascript :: code of copy button in js 
Javascript :: basic routes 
Javascript :: js wrap a function pass parameters to function 
Javascript :: koa wildcard route 
Javascript :: canvas getting created at bottom of page 
Javascript :: javascript keyup original src element 
Javascript :: typeorm cache all queries 
Javascript :: react app environment variables undefined even when starts with REACT_APP_ 
Javascript :: setImmediate clearImmediate 
Javascript :: Square Space | jquery 
Javascript :: javascript function template 
Javascript :: how to format date dd/mm/yyyy in javascript 
Javascript :: Example of Numeric Separators in es12 
Javascript :: const and let keywords in ES6 
Javascript :: convert fetch in axios 
Javascript :: import * as stringFunctions from "./string_functions.js"; // add code above this line stringFunctions.uppercaseString("hello"); stringFunctions.lowercaseString("WORLD!"); 
Javascript :: add value get value 
Javascript :: maxscript create new Layer 
Javascript :: how is react different from normal js 
Javascript :: react redux open another page 
Javascript :: return inner range recursive 
Javascript :: create filepulse connector with json 
Javascript :: how to add jquery.zoom in angular 
Javascript :: get call log in react native android 
Javascript :: codeigniter 4 tooltip dynamic 
Javascript :: serve public folder express without file extension 
Javascript :: gsheet business days 
Javascript :: upload file javascript mdn 
Javascript :: node sass version 6.0.0 is incompatible with 4.0.0 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =