We help IT Professionals succeed at work.
Get Started

How do I develop and use classes in Javascript?

380 Views
Last Modified: 2013-11-19
Hi!

I am developing a small library of functions. There may be a need where these functions could potentially interfere with each other so I need to put them into a class which than can be given an instance of.

Question is, How do I do it? And then how do I use the class?
I tried doing something like this but it does not work...
---- in the .js file ----
 
class JsLib
{
  public function Func1 ()
  {
    document.write("hello");
  }
  public function Func2 ()
  {
    document.write("bye");
  }   
}
 
 
 
--- in html, the .js is included, then ---
<script type="text/javascript>
function Exec()
{
   var a = new JsLib();
   a.Func1();
   a.Func2();
}
</script>
<a href="javascript:Exec();">test</a>

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 15 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE