JavaScript accessing methods of a class - Overclock.net - An Overclocking Community
Forum Jump: 

JavaScript accessing methods of a class

 
Thread Tools
post #1 of 6 (permalink) Old 05-21-2017, 02:17 PM - Thread Starter
New to Overclock.net
 
semnon's Avatar
 
Join Date: May 2013
Location: USA
Posts: 76
Rep: 0
Code:
class Test {
      
      foo(f) {
         console.log(f);
      }
      
      bar(b) {
         console.log(b);
      }
      
   }
   
   var fb = new Test().foo("snafu");
   
   fb.foo();

The above code produces the following error:
TypeError: Cannot read property 'f' of undefined at Object.< anonymous >

It has something to do with me adding on .foo("snafu") to the end of the instantiation. If I just create a new object like normal using var fb = new Test() then everything is fine and I can access the methods as usual.

semnon is offline  
Sponsored Links
Advertisement
 
post #2 of 6 (permalink) Old 05-22-2017, 01:17 AM
New001
 
Join Date: Feb 2016
Posts: 2,668
fb = new Test()
Test class does not have a function "f"
Code:
class Test {
  foo (f) {
    console.log(f);
    return f;
  }
}
  
(function() {    
  var fb = new Test().foo("snafu");
  console.log(fb);
})(); 

This prints 'snafu' twice (fb variable ends up holding the returned result of the .foo() function/method)
spinFX is offline  
post #3 of 6 (permalink) Old 05-22-2017, 08:25 AM
New to Overclock.net
 
Mrzev's Avatar
 
Join Date: Feb 2008
Location: Texas
Posts: 2,312
Rep: 97 (Unique: 77)
Code:
class Test {
      foo(f) {
         console.log(f);
      }    
   }

var fb = new Test().foo("snafu");

//fb = undefined
console.log(fb)  //   Undefined
fb() // fb is not a function

So... what exactly is fb? Is it the return of Test.Foo which in this case is null? Why does it not create a variable linking to the function which would make fb() a thing?



Mrzev is offline  
Sponsored Links
Advertisement
 
post #4 of 6 (permalink) Old 05-22-2017, 09:38 AM - Thread Starter
New to Overclock.net
 
semnon's Avatar
 
Join Date: May 2013
Location: USA
Posts: 76
Rep: 0
Quote:
Originally Posted by Mrzev View Post

Code:
class Test {
      foo(f) {
         console.log(f);
      }    
   }

var fb = new Test().foo("snafu");

//fb = undefined
console.log(fb)  //   Undefined
fb() // fb is not a function

So... what exactly is fb? Is it the return of Test.Foo which in this case is null? Why does it not create a variable linking to the function which would make fb() a thing?

I never instantiated an object and also chained a method to it at the same time. I thought by using var fb = new Test().foo("snafu") I could kill two birds with one stone. I thought I could create an instance of Test (via fb) and then call a method that starts an automated process. Instead of creating an anonymous function, or using the constructor, I was just experimenting with different approaches, one of which is obviously the wrong way.

semnon is offline  
post #5 of 6 (permalink) Old 05-22-2017, 09:40 AM - Thread Starter
New to Overclock.net
 
semnon's Avatar
 
Join Date: May 2013
Location: USA
Posts: 76
Rep: 0
Quote:
Originally Posted by spinFX View Post

fb = new Test()
Test class does not have a function "f"

Sorry, typo. It should be fb.foo(), not fb.f().

semnon is offline  
post #6 of 6 (permalink) Old 05-23-2017, 08:18 AM
New001
 
Join Date: Feb 2016
Posts: 2,668
Quote:
Originally Posted by semnon View Post

Sorry, typo. It should be fb.foo(), not fb.f().
oh
spinFX is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off