how we can call methods which are used in called function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sravanthi thota
    New Member
    • Jul 2016
    • 1

    how we can call methods which are used in called function

    Code:
    <script>
    function muloperator()
    {
     var val1 = parseInt(document.getElementById("txt1").value);
            var val2 = parseInt(document.getElementById("txt2").value); 
           var ans=document.getElementById("demo")
    
    }
    
      sum()
     {
          ans.value = val1+ val2;
    }
    minus()
    {
    ans.value = val1 - val2;
    }
    mul()
    {
    ans.value = val1+ val2;
    }
    div()
    {
    ans.value = val1+ val2;
    }
        </script>
    <body>
    <fieldset>
    <form>
        <input type="text" id="txt1" name="text1">
        <input type="text" id="txt2" name="text2">
    <input type="button" value="+" name="submit"><input type="button" value="-" name="submit">
    <input type="button" value="*" name="submit">
    <input type="button" value="/" name="submit">
    <input type="text" id="demo" name="demo" onclick="muloperator()"/>
    Last edited by Dormilich; Jul 7 '16, 12:49 PM. Reason: please use code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    • Sherin
      New Member
      • Jan 2020
      • 77

      #3
      Code:
      function Product(name, price) {
        this.name = name;
        this.price = price;
      }
      
      function Food(name, price) {
        Product.call(this, name, price);
        this.category = 'food';
      }
      
      console.log(new Food('cheese', 5).name);

      Comment

      • lewish95
        New Member
        • Mar 2020
        • 33

        #4
        For a start, let’s teach the user to say hello:

        Code:
        let user = {
          name: "John",
          age: 30
        };
        
        user.sayHi = function() {
          alert("Hello!");
        };
        
        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:
        Code:
        let user = {
          // ...
        };
        
        // first, declare
        function sayHi() {
          alert("Hello!");
        };
        
        // then add as a method
        user.sayHi = sayHi;
        
        user.sayHi(); // Hello!
        Last edited by gits; May 11 '20, 07:13 PM. Reason: use code tags when posting code

        Comment

        Working...