Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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"
Source by css-tricks.com #
 
PREVIOUS NEXT
Tagged: #javascript #function #class #private
ADD COMMENT
Topic
Name
2+7 =