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 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 :: Private slots are new and can be created via Private slot checks 
Javascript :: Finding Attribute value with playwright in node.js 
Javascript :: mettre en commentaire vscode 
Javascript :: @rematch/loading 
Javascript :: jumping on the clouds hackerarnk solution in javascrit 
Javascript :: binary conversion recursion in javascript 
Javascript :: typeorm sqlite Using Repositories 
Javascript :: typeorm clear cache 
Javascript :: automatically adjust color 
Javascript :: Changing Async/Await to Promises.all to Speed Up API Calls in Node.JS 
Javascript :: javascript string to date format dd/mm/yyyy 
Javascript :: javascript float not showing 0 
Javascript :: api dfetch data in reactjs 
Javascript :: mindate from another datepicker 
Javascript :: Register post meta of sidebar in wordpress 
Javascript :: js hide modal event listener name 
Javascript :: async await slow down code 
Javascript :: image support in node js chat app 
Javascript :: react native red Triangle Left 
Javascript :: react break out of useeffect 
Javascript :: sempole reguler expretion 
Javascript :: Insert tag in XML text for mixed words 
Javascript :: javascript return opposite boolean 
Javascript :: javascript covert html characters to text 
Javascript :: Fix Blurry Canvas on Mobile Phones 
Javascript :: hide parent component when child component loaded 
Javascript :: parent.containts js 
Javascript :: spreadsheetapp resize column widths 
Javascript :: how to remove elevation tab bar react native 
Javascript :: visio prevent arrows from snapping 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =