我在使用 node.js 时遇到了非常简单的初学者问题,但我似乎无法让它正常工作。
我想要的只是能够使用 index.js 文件中的“new”运算符创建对象。出于演示目的,我Person在 Person.js 文件(与我的 index.js 位于同一目录)中创建了一个简单的对象,如下所示:
class Person {
constructor() {
this._name = 'bob';
}
set name(name) {
this._name = name
}
get name() {
return this._name;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想做这样的事情(在 index.js 文件中):
var p = require('./Person.js')
var bob = new p.Person()
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息告诉我 Person 不是构造函数:
var bob = new p.Person()
^
TypeError: p.Person is not a constructor
at Object.<anonymous> (/home/.../index.js:59:11)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3) …Run Code Online (Sandbox Code Playgroud) 使用cats.Semigroup可以这样写:
import cats.Semigroup
import cats.implicits._
val l1: String Either Int = Left("error")
val r1: String Either Int = Right(1)
val r2: String Either Int = Right(2)
l1 |+| r1 // Left("error")
r1 |+| r2 // Right(3)
Run Code Online (Sandbox Code Playgroud)
我希望有一个同样惯用的运算符(类似combine),其工作方式如下:
Right我的计算中至少有一个,则返回一个RightLeft,则返回一个Left例如:
Right(1) |+| Right(2) // Right(3)
Right(1) |+| Left("2") // Right(1)
Left("1") |+| Left("2") // Left("12") // in my particular case the wrapped value here does not really matter (could also be …Run Code Online (Sandbox Code Playgroud) 目前,我正在一个交叉编译到Scala.js和普通JVM Scala的项目中。现在,我需要实现一个计时器(用于重新连接websocket),该计时器每x秒触发一次函数。可以交叉编译的计时器的良好实现是什么?
据我所知,我不能使用例如:
java.util.concurrent (不会编译为Scala.js)setTimeout和setInterval(javascript-无法从JVM Scala使用)有什么好的选择吗?或者我错了,可以使用这些吗?
使用 javascript,我可以在控制台上打印样式日志,例如:
console.log('%c Oh my heavens! ', 'background: #222; color: #bada55');
Run Code Online (Sandbox Code Playgroud)
这将导致:
有什么办法可以使用 Scala.js 做同样的事情吗?标准println似乎没有这个功能。
*编辑:当然,人们总是可以scala.scalajs.js.eval在这种情况下使用,但我更喜欢一种更“scala 原生”的方式:
js.eval("""
console.log('%c Oh my heavens! ', 'background: #222; color: #bada55');
""")
Run Code Online (Sandbox Code Playgroud) 在使用 scala-cat 的Ior数据类型时,我遇到了以下问题:
import cats._
import cats.data._
import cats.implicits._
type Locale = String
type FailureMessage = String
type Failures = NonEmptyList[FailureMessage]
private def listTranslationFiles(): IO[FailureMessage Either Array[File]] = ???
private def analyzeTranslationFiles(fs: Array[File]): Failures Ior Seq[(Locale, File)] = ???
private def readTranslations(): IO[Failures Ior Seq[(Locale, File)]] = for {
files <- listTranslationFiles()
fileIor = if(files.isLeft) (NonEmptyList(files.left.get, Nil): Failures).leftIor
else files.right.get.rightIor
// fileIor: Ior[Failures, Array[File]]
analyzed = fileIor.bimap(identity, analyzeTranslationFiles)
// analyzed: Ior[Failures, Ior[Failures, Seq[(Locale, File)]]]
result = ??? // …Run Code Online (Sandbox Code Playgroud) 在Json中,我可以这样做:
[JsonProperty("type")]
[JsonConverter(typeof(MyTpeConverter))]
public BoxType myType { get; set; }
.....
public class BoxTypeEnumConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
....
}
}
Run Code Online (Sandbox Code Playgroud)
使用XML时也可能吗?
[XmlElement("isFolder")]
[XmlConvert()] // ???
public string IsFolder { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的Xml文件有例如
....
<isFolder>t</isFolder>
....
Run Code Online (Sandbox Code Playgroud)
我希望“ t”为“ true”。
I stumbled upon this problem when trying to implement a Bifunctior type class for maps (Bifunctor[Map[_, _]]).
Bifunctor is defined like this in cats:
/**
* The quintessential method of the Bifunctor trait, it applies a
* function to each "side" of the bifunctor.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> val x: (List[String], Int) = (List("foo", "bar"), 3)
* scala> x.bimap(_.headOption, _.toLong + 1)
* res0: (Option[String], Long) = (Some(foo),4)
* }}} …Run Code Online (Sandbox Code Playgroud)