467,130 Members | 1,263 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,130 developers. It's quick & easy.

how we can call methods which are used in called function

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function muloperator()
  3. {
  4.  var val1 = parseInt(document.getElementById("txt1").value);
  5.         var val2 = parseInt(document.getElementById("txt2").value); 
  6.        var ans=document.getElementById("demo")
  7.  
  8. }
  9.  
  10.   sum()
  11.  {
  12.       ans.value = val1+ val2;
  13. }
  14. minus()
  15. {
  16. ans.value = val1 - val2;
  17. }
  18. mul()
  19. {
  20. ans.value = val1+ val2;
  21. }
  22. div()
  23. {
  24. ans.value = val1+ val2;
  25. }
  26.     </script>
  27. <body>
  28. <fieldset>
  29. <form>
  30.     <input type="text" id="txt1" name="text1">
  31.     <input type="text" id="txt2" name="text2">
  32. <input type="button" value="+" name="submit"><input type="button" value="-" name="submit">
  33. <input type="button" value="*" name="submit">
  34. <input type="button" value="/" name="submit">
  35. <input type="text" id="demo" name="demo" onclick="muloperator()"/>
Jul 7 '16 #1
  • viewed: 2665
Share:
3 Replies
Dormilich
Expert Mod 8TB
your functions don't make sense. from a sum() function anyone would expect that you pass in two values and get back their sum.

next, assigning the result is obviously not the responsibility of a calculation function.

and then there is the issue that the buttons do nothing at all.
Jul 7 '16 #2
64KB
Expand|Select|Wrap|Line Numbers
  1. function Product(name, price) {
  2.   this.name = name;
  3.   this.price = price;
  4. }
  5.  
  6. function Food(name, price) {
  7.   Product.call(this, name, price);
  8.   this.category = 'food';
  9. }
  10.  
  11. console.log(new Food('cheese', 5).name);
  12.  
May 6 '20 #3
For a start, let’s teach the user to say hello:

Expand|Select|Wrap|Line Numbers
  1. let user = {
  2.   name: "John",
  3.   age: 30
  4. };
  5.  
  6. user.sayHi = function() {
  7.   alert("Hello!");
  8. };
  9.  
  10. user.sayHi(); // Hello!
Here we’ve just used a Function Expression to create the function and assign it to the property user.sayHi of the object.

Then we can call it. The user can now speak!

A function that is the property of an object is called its method.

So, here we’ve got a method sayHi of the object user.

Of course, we could use a pre-declared function as a method, like this:
Expand|Select|Wrap|Line Numbers
  1. let user = {
  2.   // ...
  3. };
  4.  
  5. // first, declare
  6. function sayHi() {
  7.   alert("Hello!");
  8. };
  9.  
  10. // then add as a method
  11. user.sayHi = sayHi;
  12.  
  13. user.sayHi(); // Hello!
May 9 '20 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Taptu¶ | last post: by
5 posts views Thread by rbfish@hotmail.com | last post: by
5 posts views Thread by loudwinston@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.