TypeError: Cannot read property "0" from undefined

Rachael12
Tera Contributor

Hello,

I am working on a code in which I have called a function1 from function2 within same script include.
Earlier I had written it as :
function2 : function(){
     function1 (type,name);
}
Output : Code was not working and was getting error as 'ReferenceError: "function1" is not defined.'

Now I have replaced it as (it was hit and traial):
function2 : function(){ 
   //var obj = new XYZ();                 //Script include which I am currently working in and this line is commented
    obj. function1 (type,name);
}
Output : Code is working but getting error as 'TypeError: Cannot read property "0" from undefined'

Now, even though the code is working but ideally defining as obj.function1() is incorrect as far as my understanding.
So why did it work and also what does the error mean and how can I remove it.
Also even though the function was defined in 1st attempt, why did it throw as not defined.

Thank you.

3 REPLIES 3

Jaspal Singh
Mega Patron
Mega Patron

can you try using this.functionname() instead of obj.functionname() 

Chander Bhusha1
Tera Guru

Hi Rachael,

In script include we usually call the different method using this keyword say for example you have a method name function1 and other method as function2 then you have to write like this:

function1 : function(){

 

this.function2();

},

 

function2:function(){

 

//code

},

Please mark answer correct or helpful if it works fine.

Thanks,

Cb

Chander Bhusha1
Tera Guru

Hi Rachael,

I just wanted to check if you got the answer?

If it is resolved by my answer then can you please mark my answer as correct and helpful so that others can be befitted with the solution in future.

Thanks,

CB