Как написать console.log в js_of_ocaml?

Как написать console.log в js_of_ocaml? print_endline может перейти в консоль, но мне нужен доступ к трассировке, ошибкам и т. д. Могу ли я просто определить console как внешний объект?

Это не работает для меня:

let console = Js.Unsafe.global##console

Не удается с:

TypeError: N.console не является функцией test.js:255:30

Это не удается с той же ошибкой:

class type console = object
  method log : Js.js_string Js.t -> unit Js.meth
end

let _ =
  let console : console = Js.Unsafe.global##console in
  let here : Js.js_string Js.t = Js.string "here" in
  console#log here

person Olle Härstedt    schedule 24.05.2020    source источник


Ответы (1)


Объект консоли JavaScript находится внутри модуля Firebug, API здесь: https://ocsigen.org/js_of_ocaml/3.5.0/api/js_of_ocaml/Js_of_ocaml/Firebug/class-type-console/index.html

Пример:

open Js_of_ocaml
open Js
let here : js_string t = string "here" in 
Firebug.console##log here;

NB: Простое написание Firebug.console##log "here" приведет к дампу объекта, а не строки JavaScript «здесь».

person Olle Härstedt    schedule 24.05.2020