我还不熟悉ECMAScript 6.我刚刚克隆了React Starter Kit repo,它使用ES6作为应用程序代码.我很惊讶地发现linter 配置为禁止出现use strict
指令,我认为这是在ES6之前的JavaScript中推荐的.那有什么意义呢?
我有一个LoginForm组件.我要检查之前提交,这两个loginName
和password
设置.我试过这个代码(省略了很多东西):
class LoginForm extends Component {
constructor() {
super();
this.state = {
error: "",
loginName: "",
password: "",
remember: true
};
}
submit(e) {
e.preventDefault();
if(!this.state.loginName || !this.state.password) { //this is null
this.setState({ error: "Fill in both fields" });
} else {
console.log("submitting form");
}
}
render() {
return (
<div className="col-xs-12 col-sm-6 col-md-4">
<form className="login" onSubmit={this.submit}>
<button type="submit" className="btn btn-default">Sign in</button>
</form>
</div>
);
}
}
export default LoginForm;
Run Code Online (Sandbox Code Playgroud)
但是,我TypeError
在事件处理程序中得到一个,说this
是null.
我该怎么办?
我正在尝试与龙卷风服务器建立WS连接.服务器代码很简单:
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def open(self):
print("WebSocket opened")
def on_message(self, message):
self.write_message(u"You said: " + message)
def on_close(self):
print("WebSocket closed")
def main():
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static")
}
app = tornado.web.Application([
(r'/ws', WebSocketHandler),
(r"/()$", tornado.web.StaticFileHandler, {'path':'static/index.html'}),
], **settings)
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Run Code Online (Sandbox Code Playgroud)
我从这里复制粘贴客户端代码:
$(document).ready(function () {
if ("WebSocket" in window) {
console.log('WebSocket is supported by your browser.');
var serviceUrl = 'ws://localhost:8888/ws';
var protocol = 'Chat-1.0';
var socket = new WebSocket(serviceUrl, protocol);
socket.onopen = function () {
console.log('Connection Established!');
};
socket.onclose …
Run Code Online (Sandbox Code Playgroud) 我想const validation
根据条件分配给我一个值.
如果this.showRequired() == true
那应该是'required'
否则this.showError() == true
它应该是'error'
.
我当然知道我可以将它提取到函数或级联三元运算符,但第一个看起来代码膨胀,后者丑陋.
有没有更好的方法呢?if-else表达式可能吗?
我理解Angular将指令名从camel case转换为连字符分隔的字符串.
是否可以通过API访问此功能,如功能或其他内容?我想在我的角应用程序中将一些字符串转换为带连字符的版本,如果不是必要的话我不想重新发明轮子.
在Linux上,sched.h
包含的定义
int sched_rr_get_interval(pid_t pid, struct timespec * tp);
获得流程的时间片.但是,OS X El Capitan附带的文件不符合该定义.
在OS X上有替代方案吗?
我对Slick来说是全新的.我试图创建一个基本的表类型,但它只是不编译.这是我的代码:
import scala.slick.driver.PostgresDriver._
import scala.slick.lifted.Tag
import scala.slick.lifted.Column
import scala.slick.lifted.ProvenShape
class Documents(tag: Tag) extends Table[(Long, String, String)](tag, "DOCUMENTS") {
def id: Column[Long] = column[Long]("ID", O.PrimaryKey)
def `type`: Column[String] = column[String]("TYPE")
def data: Column[String] = column[String]("DATA")
def * : ProvenShape[(Long, String, String)] = (id, `type`, data)
}
Run Code Online (Sandbox Code Playgroud)
我收到这些错误:
<console>:13: error: could not find implicit value for parameter tm: scala.slick.ast.TypedType[Long]
def id: Column[Long] = column[Long]("ID", O.PrimaryKey)
^
<console>:14: error: could not find implicit value for parameter tm: scala.slick.ast.TypedType[String]
def `type`: Column[String] = column[String]("TYPE") …
Run Code Online (Sandbox Code Playgroud) 我很难理解何时使用星号运算符进行解除引用以及何时可以省略它.
fn main() { a(); b(); c(); d(); }
fn a() {
let v = 1;
let x = &v;
println!("a {}", *x);
println!("a {}", 1 + *x);
}
fn b() {
let v = 1;
let x = &v;
println!("b {}", x);
println!("b {}", 1 + x);
}
fn c() {
let mut v = 1;
let mut x = &mut v;
println!("c {}", *x);
println!("c {}", 1 + *x);
}
fn d() {
let mut v = 1; …
Run Code Online (Sandbox Code Playgroud) 如何在 Gradle Scala 项目中指定 Scala 版本?
在官方文件指出,scala-library
依赖被用来获取编译器的保持。
dependencies {
compile 'org.scala-lang:scala-library:2.11.1'
}
Run Code Online (Sandbox Code Playgroud)
这是否意味着将使用 Scala 2.11.1?
我有一个defrecord
名为ConstraintLookup
的sre.plan.dsl.constraint
命名空间.我想在gen-class
放置在sre.plan.compiler
命名空间的方法中使用其生成的类:
(ns sre.plan.compiler
(:require
[sre.plan.dsl.constraint :as constraint])
(:import (sre.plan.dsl.constraint ConstraintLookup))
(:gen-class
:name sre.plan.Compiler
:methods [^:static [makeConstraintLookupFromTargetsAndBounds
[Iterable Iterable] ConstraintLookup]]))
Run Code Online (Sandbox Code Playgroud)
我用nebula-clojure
插件和Gradle进行AOT编译.编译器遇到ns声明时会发出错误:
> Task :sre:compileClojure
Exception in thread "main" java.lang.ClassNotFoundException: java.lang.ConstraintLookup, compiling:(sre/plan/compiler.clj:1:1)
Run Code Online (Sandbox Code Playgroud)
类似地,当sre.plan.dsl.constraint.Constraint
在方法声明中使用完全限定时,我得到:
Exception in thread "main" java.lang.ClassNotFoundException: sre.plan.dsl.constraint.ConstraintLookup, compiling:(sre/plan/compiler.clj:1:1)
Run Code Online (Sandbox Code Playgroud)
这里有什么问题?我搞不清楚了.
更新:
引用的ns看起来像这样:
(ns sre.plan.dsl.constraint
(:require [clojure.set :refer :all]
[clojure.algo.generic.functor :refer :all]))
(defrecord ConstraintLookup [free bound])
Run Code Online (Sandbox Code Playgroud)
更新:
在我看来,无论如何,在gen-class中你必须使用完全限定的类名.但是我仍然不明白为什么具有完全限定名称的版本不起作用.
cats中是否有将Set
其转换为的标准扩展方法Option[cats.data.NonEmptySet]
?
Scalatest是否提供和的匹配器NaN
?
如果可以的话,我想使这些断言更具描述性:
Double.NaN.isNaN shouldBe true
Double.NegativeInfinity.isInfinite shouldBe true
Run Code Online (Sandbox Code Playgroud) 我想解组string
包含JSON的内容,但是该Unmarshal
函数需要[]byte
输入.
如何将我的UTF8转换string
为[]byte
?