我正在尝试使用SICP,我得到了一些代码.所以我开始:
#lang scheme
(word 'comp 'uter)
Run Code Online (Sandbox Code Playgroud)
返回错误:函数(字)未定义.
即使我试图将其复制到IDE(运行):
(define word?
(let ((number? number?)
(symbol? symbol?)
(string? string?))
(lambda (x)
(or (symbol? x) (number? x) (string? x)))))
Run Code Online (Sandbox Code Playgroud)
还是一样.
我认为这可能是语言版本的某些问题或者其他问题.
以上是"简单方案",当我在SICP中引入代码时:
(define (sqrt x)
(sqrt-iter 1.0 x))
Run Code Online (Sandbox Code Playgroud)
IDE返回sqrt-iterundefined.代码可以在第一章找到:http://mitpress.mit.edu/sicp/code/index.html
我想在我的客户端程序中创建一个自定义事件发射器.我正在引用EventTarget的这个(稀疏)文档
我的实施尝试
var Emitter = function Emitter() {
EventTarget.call(this);
};
Emitter.prototype = Object.create(EventTarget.prototype, {
constructor: {
value: Emitter
}
});
Run Code Online (Sandbox Code Playgroud)
我想要的用法
var e = new Emitter();
e.addEventListener("hello", function() {
console.log("hello there!");
});
e.dispatchEvent(new Event("hello"));
// "hello there!"
Run Code Online (Sandbox Code Playgroud)
哪里失败了
var e = new Emitter();
// TypeError: Illegal constructor
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
更新
以下是可能的,但它是一个依赖于虚拟DOMElement的hack
var fake = document.createElement("phony");
fake.addEventListener("hello", function() { console.log("hello there!"); });
fake.dispatchEvent(new Event("hello"));
// "hello there!"
Run Code Online (Sandbox Code Playgroud)
我想知道如何在不使用虚拟元素的情况下完成此操作
我试图用CoffeeScript重新创建这个流行的jQuery lambda闭包:
(function($, window, undefined){
$(document).ready(function(){
...
});
})(jQuery, window);
Run Code Online (Sandbox Code Playgroud)
到目前为止我有这个:
(($, window, undefined) ->
$ ->
alert "js!"
)(jQuery, window)
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
错误:第1行的解析错误:意外的'BOOL'
看起来这undefined就是问题的原因.关于如何解决这个问题的任何想法?
我正在尝试启动mplayer.我的文件名包含空格,这些应该被转义.这是我正在使用的代码:
@player_pid = fork do
exec "/usr/bin/mplayer #{song.file}"
end
Run Code Online (Sandbox Code Playgroud)
其中#{song.file}包含一条路径"/home/example/music/01 - a song.mp3".如何正确地转义此变量(以及标题可能包含的其他奇怪字符),以便终端接受我的命令?
我正在尝试创建一个虚拟套接字,用于我的一些测试
var net = require("net");
var s = new net.Socket();
s.on("data", function(data) {
console.log("data received:", data);
});
s.write("hello!");
Run Code Online (Sandbox Code Playgroud)
得到这个错误
错误:此套接字已关闭.
我也试过创建套接字
var s = new net.Socket({allowHalfOpen: true});
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
作为参考,完整的测试看起来像这样
it("should say hello on connect", function(done) {
var socket = new net.Socket();
var client = Client.createClient({socket: socket});
socket.on("data", function(data){
assert.equal("hello", data);
done();
});
client.connect();
// writes "hello" to the socket
});
Run Code Online (Sandbox Code Playgroud) 除了常规功能和内置功能之外,还有一种优雅的方式可以告诉Harmony的纤细箭头功能吗?
在和谐维基指出:
箭头函数就像内置函数一样缺乏.prototype和任何[[Construct]]内部方法.所以new(()=> {})抛出一个TypeError,否则箭头就像函数一样
这意味着,您可以测试箭头功能,如:
!(()=>{}).hasOwnProperty("prototype") // true
!(function(){}).hasOwnProperty("prototype") // false
Run Code Online (Sandbox Code Playgroud)
但是测试也将返回true任何内置函数,例如setTimeout或Math.min.
如果您获得源代码并检查它是否可以在Firefox中运行"native code",但它看起来不太可靠也不可移植(其他浏览器实现,NodeJS/iojs):
setTimeout.toSource().indexOf("[native code]") > -1
Run Code Online (Sandbox Code Playgroud)
小的GitHub项目node-is-arrow-function依赖于函数源代码的RegExp检查,这不是那么整洁.
编辑:我尝试了一下JavaScript解析器橡子,看起来工作得很好 - 即使它太过分了.
acorn = require("./acorn");
function fn_sample(a,b){
c = (d,e) => d-e;
f = c(--a, b) * (b, a);
return f;
}
function test(fn){
fn = fn || fn_sample;
try {
acorn.parse("(" + fn.toString() + ")", {
ecmaVersion: 6,
onToken: function(token){ …Run Code Online (Sandbox Code Playgroud) javascript function ecmascript-harmony ecmascript-6 arrow-functions
是否有理由不在中间件中为处理程序使用箭头而不是常规函数表达式?
app.use(mountSomething())
router.use(mountSomethingElse())
app.get('/', (req,res,next)=> {
next();
})
route.get('/path', (req,res,next)=>{
res.send('send')
})
Run Code Online (Sandbox Code Playgroud) 这是我的Angular代码中的普通ES5函数,它可以工作:
app.run(function($templateCache){ $templateCache.put('/some','thing') });
Run Code Online (Sandbox Code Playgroud)
我想将它转换为ES6箭头功能
app.run($templateCache => $templateCache.put('/some','thing'));
Run Code Online (Sandbox Code Playgroud)
但它给出了错误
Uncaught Error: [$injector:unpr] Unknown provider: '/some'Provider <- '/some'
http://errors.angularjs.org/1.4.6/$injector/unpr?p0='%2Fsome'Provider%20%3C-%20'%2Fsome'
REGEX_STRING_REGEXP @ angular.js:68
(anonymous function) @ angular.js:4287
getService @ angular.js:4435
(anonymous function) @ angular.js:4292
getService @ angular.js:4435
invoke @ angular.js:4467
(anonymous function) @ angular.js:4297
forEach @ angular.js:336
createInjector @ angular.js:4297
doBootstrap @ angular.js:1657
bootstrap @ angular.js:1678
angularInit @ angular.js:1572
(anonymous function) @ angular.js:28821
trigger @ angular.js:3022
eventHandler @ angular.js:3296
Run Code Online (Sandbox Code Playgroud)
ES6箭头功能是否与Angular不兼容?
编辑:我想也许Angular无法推断名称$templateCache,因此无法注入它,但后来我将其记录到控制台,它确实显示正确:
app.run($templateCache=>console.log($templateCache));
// =>
// Object {}
// destroy: function() …Run Code Online (Sandbox Code Playgroud) javascript angularjs ecmascript-6 angularjs-injector arrow-functions
我今天遇到了一个问题,请考虑以下组件:
export default class Input extends React.Component {
someFunction() {
console.log(this.props.value)
}
render () {
const { type, value, required } = this.props
return (
<div className={cx('Input')}>
<input type={type} value={value} required={required} />
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
我成功地破坏了,this.props并且可以在渲染中使用它们,但是如果我需要在它之外使用道具值,即内部someFunction()我不确定如果我移出constant { ... }并包括export default class Input extends React.Component {一行一行会产生什么后果.这仍然有效吗?
我有这样的路线
<Route path="/search" component={Search}>
Run Code Online (Sandbox Code Playgroud)
基本Search组件看起来像这样
class Search extends React.Component {
constructor (props) {
super(props)
this.state = {query: ''}
}
handleSubmit (event) {
event.preventDefault()
this.setState({query: this.refs.queryInput.value})
}
renderSearchResult() {
if (this.state.query === '')
return <EmptySearchResult />
else
return <SearchResult query={this.state.query}/>
}
render() {
return (
<div className="searchContainer">
<div className="row">
<div className="search">
<form onSubmit={event => this.handleSubmit(event)}>
<input className="searchInput" placeholder="robocop" ref="queryInput" />
</form>
</div>
</div>
<div className="row">
{this.renderSearchResult()}
</div>
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
SearchResult 中继容器看起来像这样
class SearchResult extends React.Component {
render() …Run Code Online (Sandbox Code Playgroud) functional-programming relay react-router graphql react-router-relay