Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - How do you call a function inside a class from within that class?

Tags:

I am trying to call the function MyMethod from within a object but none of the syntax below works. There must be a really obvious error below but I can't see it.

<html xmlns="http://www.w3.org/1999/xhtml"> <head>     <script type="text/jscript">       function MyObject() {         //how do I get one of these to work??        this.MyMethod; //does not work        this.MyMethod(); //does not work either        MyMethod(); //does not work either         this.MyMethod = function () {          alert('It works');        }      }       var test = new MyObject();     </script>  </head> <body>  </body> </html> 
like image 724
Petras Avatar asked Aug 22 '10 11:08

Petras


People also ask

How do you call a function inside a class?

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.

How do you call a function using a class in the JavaScript?

The JavaScript call() Method The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

Can you call a function from within it?

Calling a function from within itself is called recursion and the simple answer is, yes.

How do you call a function inside an object in JavaScript?

You can call a function inside an object by declaring the function as a property on the object and invoking it, e.g. obj. sum(2, 2) . An object's property can point to a function, just like it can point to a string, number or other values. Copied!


2 Answers

There are two main problems

  1. The MIME type is text/javascript, not text/jscript
  2. You are defining the method after you try to call it

So:

  function MyObject() {     this.MyMethod = function () {       alert('It works');     }     this.MyMethod(); //should now work   }    var test = new MyObject(); 
like image 103
Quentin Avatar answered Oct 01 '22 15:10

Quentin


The 2 ways of defining a function provide different accessibilities

First, setting it a property of the parent function, as is done in the "A" version of your script below. If you do this, the function is only usable after you give the definition.

Second, defining the function with the classical approach of "function functionName () { ... }". This definition undergoes "hoisting", which means the function becomes available throughout the parent object, i.e. even above the place it is defined. You can see this in the "B" version in the sample below, based on the original poster's code. Also on https://jsfiddle.net/dnL4j30u/

<script>   function MyObject() {      MyMethod();       this.MyMethod = function() {       console.log('It works A');     }      this.MyMethod();       MyMethod();       function MyMethod() {       console.log('It works B');     }    }   var test = new MyObject(); </script> 

The output is

 It works B       (this works because the second definition gets hoisted)  It works A  It works B 
like image 43
Eureka Avatar answered Oct 01 '22 14:10

Eureka