Reputation: 1403
Why am I getting the error: "Uncaught TypeError: self.myTest is not a function"? How do I call a method from within another method in a javascript class?
class MyClass {
myTest() {
console.log('it works');
}
runMyTest() {
self.myTest();
}
}
var myClass = new MyClass();
myClass.runMyTest();
Upvotes: 64
Views: 124694
Reputation: 277
class MyClass {
myTest() {
console.log('it works');
}
runMyTest = ()=>{
this.myTest();
}
runMyTest2 = function(){
this.myTest();
}
}
var myClass = new MyClass();
myClass.runMyTest();
myClass.runMyTest2();
use arrow function to bind this to the global object. To represnt objects properties we have to use function
let user = {
name :"Something"
}
user.value = ()=>{
console.log(this)
}
user.value2 = function(){
console.log(this)
}
user.value(); ///returns this for windows
user.value2(); ///returns object user
Upvotes: 5
Reputation: 647
The below is an ideal way to call a function in the same class. if it is NodeJS.
NodeJS
var self;
class SampleClass {
constructor(tableName) {
self = this;
}
secondFunction(param) {
console.log(`called secondFunction with ${param}`)
}
firstFunction(param) {
console.log(`called firstFunction with ${param}`)
self.secondFunction(param);
}
async callableFunction(param) {
self.firstFunction(param)
}
};
module.exports = SampleClass
ES-6
class SampleClass {
constructor(tableName) {
//empty constructor or fill with what you need on init
}
secondFunction(param) {
console.log(`called secondFunction with ${param}`)
}
firstFunction(param) {
console.log(`called firstFunction with ${param}`)
this.secondFunction(param);
}
async callableFunction(param) {
this.firstFunction(param)
}
};
var SampleClassObject = new SampleClass()
SampleClassObject.callableFunction('Argument as string!')
Upvotes: -1
Reputation: 9
class MyClass(){
constructor(){
this.init();
}
init(){
const $this = this;
this.doSomethigElse();
}
doSomethingElse(){
console.log("It worked!");
}
}
Upvotes: -1
Reputation: 1
class MyClass {
myTest() {
console.log('it works');
}
runMyTest() {
this.myTest();
}
}
var myClass = new MyClass();
myClass.runMyTest();
Upvotes: -1
Reputation: 8215
You need to use the this
keyword instead of self
.
runMyTest() {
this.myTest();
}
A side note
If you are nesting standard functions notation then this
is not lexically bound (will be undefined). To get around this, use Arrow Functions (preferred), .bind
, or locally define this
outside of the function.
class Test {
constructor() {
this.number = 3;
}
test() {
function getFirstThis() {
return this;
}
const getSecondThis = () => {
return this;
};
const getThirdThis = getFirstThis.bind(this);
const $this = this;
function getFourthThis() {
return $this;
}
// undefined
console.log(getFirstThis());
// All return "this" context, containing the number property
console.log(this);
console.log(getSecondThis());
console.log(getThirdThis());
console.log(getFourthThis());
}
}
new Test().test();
Upvotes: 67
Reputation: 937
You need to use this
not self
like
runMyTest() {
this.myTest();
}
However a lot of implementations like to keep the reference and are doing the following:
var self = this;
That might be the reason you were thinking of self
as self reference. For further reading I'd suggest this SO - post
Upvotes: 12
Reputation:
Other solution is save the value of variable context $this
inside other for example in this
and for use is this.anyFunction();
class MyClass {
myTest() {
console.log('it works');
}
runMyTest() {
let this=$this;
this.myTest();
}
}
Upvotes: -2
Reputation: 83
class MyClass {
myTest() {
console.log('it works');
}
runMyTest() {
this.myTest();
}
}
var myClass = new MyClass();
myClass.runMyTest();
Upvotes: 3