Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
161
rated 0 times [  165] [ 4]  / answers: 1 / hits: 7507  / 10 Years ago, wed, march 19, 2014, 12:00:00

I am interested in a portable way of writing a string to standard output, without implicit newlines added to the end, ideally forcing encoding to UTF-8, that works with either of




  • jrunscript (from any JDK)

  • Rhino

  • node.js



My current code tries to detect where it is running, then uses a platform-specific write method:



  if (typeof process !== undefined) {     // assume node.js
var log = function(string) {process.stdout.write(string);};
}
else if (typeof println == undefined) { // assume rhino
var log = function(string) {java.lang.System.out.write(java.lang.String(string).getBytes(utf-8));};
}
else { // assume jrunscript
var log = function(string) {java.lang.System.out.print(string);};
}
log(X);
log(Y);


It should result in:



  XY


Can this be done better?



For jrunscript, I had been using function print, but that changed its behavior in JDK-8 on behalf of JDK-8021773.


More From » node.js

 Answers
1

I am interested in a portable way of writing a
string to standard output, without implicit newlines added to the end, ideally forcing
encoding to UTF-8, that works with either of...




there is none,since javascript doesnt have system apis. JS core is limited and you need to use the api available within the environment javascript engine is running in.




My current code tries to detect where it is running, then uses a
platform-specific write method. But I'd rather get rid of that.




that's the only thing you can do.



EDIT : the javascript spec is here : http://es5.github.io/ and it's short,there is 0 notion of standard output or standard input because there is no javascript standard library.


[#46717] Tuesday, March 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austonjuancarlosb

Total Points: 238
Total Questions: 89
Total Answers: 99

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;