Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Declaring A Method Outside The Constructor

/*for methods declared outside the constructor remember:
do not add a function/const..it is fn= ()=>{} and NOT const fn = ()=>{} if outside the constructor
access them in the constructor with this.methodName;
the same method CAN access this.name below.
They (method declared inside and outside) work.

*/
class Person
 {
constructor(name)
{
this.name = name;
const result = this.fn();
return result;
}

fn = async ()=>{
let r =await fetch('/test', {method: 'POST', body: JSON.stringify({name:this.name}), headers: {'Content-type': 'application/json; charset=UTF-8'}})
return  r.json();
} 
}

let p = 	 new Person("john smith");
let result = await p;

console.log(result.name);
}
 
PREVIOUS NEXT
Tagged: #Declaring #A #Method #Outside #The #Constructor
ADD COMMENT
Topic
Name
8+3 =