Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript function as class new private

// Javascript Function that behaves as class, with private variables
function Person(name){
    const birth = new Date();
    this.greet = () => `Hello my name is ${name}. I was born at ${birth.toISOString()}`;
}

const joe = new Person("Joe");
joe.greet(); // Hello my name is Joe. I was born at 2021-04-09T21:12:33.098Z
// The birth variable is "inaccessible" from outside so "private"
Comment

js class private

class ClassWithPrivateField {
  #privateField;
  
  constructor() {
    this.#privateField = 42;
    this.#randomField = 666; # Syntax error
  }
}

const instance = new ClassWithPrivateField();
instance.#privateField === 42; // Syntax error
Comment

js es6 class private method

class MyClass
{
  	// Prepend hash to make it private. Browser support at:
  	//	https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields#browser_compatibility
    // Dynamic access (ie: this.#[fieldName]) is disallowed by design.
  	#myPrivateMethod() {
    	// do private things
	}
}
Comment

private class members javascript

class Person {
    constructor(name) {
        var _name = name
        this.setName = function(name) { _name = name; }
        this.getName = function() { return _name; }
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: split and convert a string into object 
Javascript :: reverse a string javascript 
Javascript :: import npm module node.js 
Javascript :: why did you render 
Javascript :: ismobile react 
Javascript :: expression vs statement javascript 
Javascript :: react js docker 
Javascript :: print an object in javascript 
Javascript :: convert % to px javascript 
Javascript :: convert data image url into an image file 
Javascript :: how sum all array element with for 
Javascript :: chart js x axis data bar 
Javascript :: postman environment variables 
Javascript :: password reset passport-local mongoose 
Javascript :: overflowx javascript 
Javascript :: make dots in three js 
Javascript :: video conferencing app with html and js 
Javascript :: js format date 
Javascript :: random trong js 
Javascript :: learn nodejs 
Javascript :: onclick node js 
Javascript :: javascript arreglos 
Javascript :: react controlled input 
Javascript :: google places autocomplete empty latitude 
Javascript :: jquery slider move event 
Javascript :: grid in chart.js 
Javascript :: date to string format javascript 
Javascript :: regex js 
Javascript :: angular.fromJson 
Javascript :: js subtract days 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =