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();
Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile. And in java 8 and newer version you achieve it by lambda expression.
To call a function within class with Python, we call the function with self before it. We call the distToPoint instance method within the Coordinates class by calling self.
To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ). A class must have a matching filename ( Main and Main. java).
A method must be created in the class with the name of the method, followed by parentheses (). The method definition consists of a method header and method body. We can call a method by using the following: method_name(); //non static method calling.
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();
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
class MyClass {
myTest() {
console.log('it works');
}
runMyTest() {
this.myTest();
}
}
var myClass = new MyClass();
myClass.runMyTest();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With