0
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>latihan 2 Objek</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

    </head>
    <body>
        <script type="text/javascript">
         function timeObjc(hour,minute,second){
                this.hour= hour;
                this.minute = minute;
                this.second= second;       
                this.setTime= function(whathour,whatminute,whatscnd){
                                    this.hour=whathour;
                                    this.minute =whatminute;
                                    this.second =whatscnd;
                                };
                this.runTime = function(){return this.hour+":"+this.minute+":"+this.second};
            }
            timeObjc.setTime(10,22,36);
            timeObjc.runTime();

        </script>
    </body>
</html>

i want to set the time manually outside the function and print it with the runTime functio, but it keep error that said "setTime is not a function".. i would be grateful to know wht i'm missing and why is it happening. thanks in advance

Fidel
  • 862
  • 7
  • 13
  • You need to create a new timeObjc and assign the object to a variable. var newTimeObjc = new timeObjc(); newTimeObjc.setTime(10,22,36); – codemax Jun 30 '17 at 08:27
  • you try to use static construction without using its syntax. Your syntax is a constructor function, you need then to instantiate it. To use a static context, you have to add the memeber functions outside of the "class" function, using its name (which is a bit strange) – Kaddath Jun 30 '17 at 08:28
  • @Kaddath thanks it fixed, but how if i want to make multiple time model (24 and 12) through the argument of run time function? – Fidel Jun 30 '17 at 08:38

2 Answers2

0

You haven't initialized your timeObjc yet. And you're trying to run setTime as a static method, which it's not.

You have to first initialize the object, then run its setTime function.

function timeObjc(hour,minute,second){
    this.hour= hour;
    this.minute = minute;
    this.second= second;       
    this.setTime= function(whathour,whatminute,whatscnd){
                        this.hour=whathour;
                        this.minute =whatminute;
                        this.second =whatscnd;
                    };
    this.runTime = function(){return this.hour+":"+this.minute+":"+this.second};
}
var obj = timeObj(1,1,1)
obj.setTime(10,22,36);
obj.runTime();
shotor
  • 763
  • 6
  • 17
  • thanks it fixed, but how if i want to make multiple time model (24 and 12) through the argument of run time function? – Fidel Jun 30 '17 at 08:35
  • You could add a parameter to the `runTime` function. `this.runTime = function(format) { format = format || 24; if (format === 24) { ... } else { ... } }` Does that make sense? – shotor Jun 30 '17 at 08:46
  • it should be changing the hour format between 12 and 24 hour depend on which we choosing.. e.g if i want print 12:20:33 in 24 hour format and i call the runtime function and the paramater is 24, it should be automatically changing the hour to be 00:20:33.. any idea? – Fidel Jun 30 '17 at 08:53
  • Check out this stackoverflow for a sure fire way to do it: https://stackoverflow.com/questions/4898574/converting-24-hour-time-to-12-hour-time-w-am-pm-using-javascript – shotor Jun 30 '17 at 09:43
0

Create new object using new keyword.

like var timeObject = new timeObjc(11, 22, 33) then timeObjc.setTime(2,3,4)

ricky
  • 1,644
  • 1
  • 13
  • 25