0

al introducir en la consola del navegador const sw = new Stopwatch(); la consola tira un error para el metodo start, porque?. aqui les dejo mi código javascript. No entiendo porque dice que el metodo start no es una función ayuda por favor.

function Stopwatch()
{
    let startTime, endTime, running, duration = 0;

this.start() = function()
{
    if(running) 
    {
        throw new Error('Stopwatch has already started.');
    }

    running = true;
    startTime = new Date();
};

this.stop() = function()
{
    if(!running)
    {
        throw new Error('Stopwatch is not started.');
    }

    running = false;
    endTime = new Date();
    const seconds = (endTime.getTime() - startTime.getTime())/1000;
    duration += seconds;
};

this.reset() = function() 
{
    startTime = null;
    endTime = null;
    running = false;
    duration = 0;
};

Object.defineProperty(this, 'duration', {
    get: function() { return duration; }
});
};

1 respuesta 1

0

Tal y como dice el error, start, stop y reset no son funciones, estás agregando paréntesis como si lo fueran, posteriormente señalas que es igual a una función, intenta quitando los paréntesis de la siguiente manera:

function Stopwatch()
{
    let startTime, endTime, running, duration = 0;

this.start = function()
{
    if(running) 
    {
        throw new Error('Stopwatch has already started.');
    }

    running = true;
    startTime = new Date();
};

this.stop = function()
{
    if(!running)
    {
        throw new Error('Stopwatch is not started.');
    }

    running = false;
    endTime = new Date();
    const seconds = (endTime.getTime() - startTime.getTime())/1000;
    duration += seconds;
};

this.reset = function() 
{
    startTime = null;
    endTime = null;
    running = false;
    duration = 0;
};

Object.defineProperty(this, 'duration', {
    get: function() { return duration; }
});
};


const sw = new Stopwatch();
//sw.start();
//sw.stop();
//sw.reset();

Una vez que colocas que es igual a una función entonces si podrás acceder a dichas funciones después de declarar el objeto de la siguiente manera, ejemplo: sw.start();, dejé los ejemplos comentados en el código.

Espero te ayude. Saludos.

8
  • no en realidad cundo pongo sw.start(); me sale esto VM257:1 Uncaught ReferenceError: sw is not defined at <anonymous>:1:1 el 8 may. 2019 a las 21:44
  • y ya defini el objeto entonces no se que pasa el 8 may. 2019 a las 21:44
  • si removiste los paréntesis como marqué en el ejemplo? el 8 may. 2019 a las 21:51
  • ah perdon ahora los removi pero el problema ahora es que le doy a sw.start; luego de un tiempo sw.stop; y al final sw.duration; y me da duration = 0 eso no esta bien :( el 8 may. 2019 a las 22:00
  • no me corre el start porque si le aplicara start 2 veces seguidas deberia tirarme un mensaje de error lo cual tampoco pasa. el 8 may. 2019 a las 22:06

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.