Reputation: 45
is it possible to have "this" and "return" in one function? Like this?
I call the function often with different parameters. But as I understand strict mode, you have to initiate the function for every parameter with "new convertSomething". Is there a shorter way? Thanks
"use strict";
function convertSomething(convertSeconds){
let minutes = Math.floor(convertSeconds / 600) % 600;
let seconds = Math.trunc(convertSeconds % 600 / 10);
let milliseconds = (convertSeconds % 600)%10;
this.milliseconds = milliseconds;
this.minutes = minutes;
this.seconds = seconds;
let formattedTime = minutes + ":" + seconds + "." + milliseconds;
this.formattedTime = formattedTime;
return formattedTime;
}
let showTime = new convertSomething(2005);
console.log(showTime.milliseconds);
console.log(showTime.formattedTime);
console.log(convertSomething(1000)); // ERROR: this is undefined
<html>
<head>
<meta charset="ISO-8859-1">
<script src="returnthis.js"></script>
</head>
<body>
</body>
</html>
Upvotes: 0
Views: 97
Reputation: 1490
Even if the reason for doing that is questionable, you can do this
:
function convertSomething(convertSeconds){
let minutes = Math.floor(convertSeconds / 600) % 600;
let seconds = Math.trunc(convertSeconds % 600 / 10);
let milliseconds = (convertSeconds % 600)%10;
let formattedTime = minutes + ":" + seconds + "." + milliseconds;
if(this){
this.milliseconds = milliseconds;
this.minutes = minutes;
this.seconds = seconds;
this.formattedTime = formattedTime;
}
return(formattedTime);
}
EDIT
Another solution would be this one:
function convertSomething(convertSeconds,details){
let minutes = Math.floor(convertSeconds / 600) % 600;
let seconds = Math.trunc(convertSeconds % 600 / 10);
let milliseconds = (convertSeconds % 600)%10;
let formattedTime = minutes + ":" + seconds + "." + milliseconds;
if(details){
details.milliseconds = milliseconds;
details.minutes = minutes;
details.seconds = seconds;
details.formattedTime = formattedTime;
}
return(formattedTime);
}
Then you can call the function for everything:
var time = convertSomething(2500);
var details = {};
var time2 = convertSomething(1000,details);
console.log(details.minutes) // or anything else
EDIT 2
Generally, class, functions, methods are meant to do one and only one thing. In the case you were creating an API, using a function both as a function and a constructor can be very confusing for someone using it. For those reasons, the second option I gave you maybe the best approach to use.
Upvotes: 2
Reputation: 225054
Right now, your function is acting as both a constructor and a regular function, and those tasks are completely separate. When you use it with new
, the return value is ignored (because it’s a primitive); when you use it without new
in sloppy mode, the properties are put on the global object and ignored.
A cleanup that keeps the function doing both jobs would be to return an object containing all your desired properties and another one for a formatted string:
"use strict";
function convertSomething(convertSeconds) {
let minutes = Math.floor(convertSeconds / 600) % 600;
let seconds = Math.trunc(convertSeconds % 600 / 10);
let milliseconds = (convertSeconds % 600)%10;
let formattedTime = minutes + ":" + seconds + "." + milliseconds;
return {
minutes,
seconds,
milliseconds,
formattedTime,
};
}
let showTime = convertSomething(2005);
console.log(showTime.milliseconds);
console.log(showTime.formattedTime);
console.log(convertSomething(1000).formattedTime);
A function doing lots of unrelated jobs isn’t a good thing, though. I’d recommend splitting this one out into one function to convert a number of deciseconds (that’s what you seem to have, so the other names convertSeconds
and milliseconds
are misleading) to minutes, seconds, and deciseconds, and one function to format that.
function timestampFromDeciseconds(ds) {
return {
minutes: Math.trunc(ds / 600),
seconds: Math.trunc(ds % 600 / 10),
deciseconds: ds % 10,
};
}
function formatTimestamp(timestamp) {
return `${timestamp.minutes}:${timestamp.seconds}.${timestamp.deciseconds}`;
}
Then you can use them like this:
"use strict";
function timestampFromDeciseconds(ds) {
return {
minutes: Math.trunc(ds / 600),
seconds: Math.trunc(ds % 600 / 10),
deciseconds: ds % 10,
};
}
function formatTimestamp(timestamp) {
return `${timestamp.minutes}:${timestamp.seconds}.${timestamp.deciseconds}`;
}
let showTime = timestampFromDeciseconds(2005);
console.log(showTime.deciseconds);
let otherTime = timestampFromDeciseconds(1000);
console.log(formatTimestamp(otherTime));
Upvotes: 1