小编Lon*_*ner的帖子

为什么即使左侧没有定义新变量,else if 语句中的短变量声明也不会编译失败?

以下代码预计会在编译时失败并出现错误:

package main

import (
    "fmt"
)

func main() {
    x := 10
    x := x + 1
    fmt.Println(x)
}
Run Code Online (Sandbox Code Playgroud)

编译错误是:

./prog.go:9:4: no new variables on left side of :=
Run Code Online (Sandbox Code Playgroud)

所以我期待这段代码也会因错误而失败:

./prog.go:9:4: no new variables on left side of :=
Run Code Online (Sandbox Code Playgroud)

这是输出:

else if block: x:  11
Run Code Online (Sandbox Code Playgroud)

为什么即使:=操作符 inelse if x := x + 1没有定义任何新变量,第二个程序也会成功?

syntax grammar go semantics

4
推荐指数
1
解决办法
301
查看次数

.appendChild()是否会终止子项与其当前父项之间的父子关系?

当我做类似的事情时divNode.appendChild(pNode),它pNode被移到了里面divNode.它的父母似乎从它的变化divNode.这是预期的行为吗?

这是我的代码:http://jsfiddle.net/YqG96/

<html>
<head>
<style>
p {
    background: orange;
}
div {
    background: lightcyan;
    padding: 5px;
}
</style>
<script>
window.onload = function() {
    pNode = document.getElementById('pNode');
    divNode = document.getElementById('divNode');
    divNode.appendChild(pNode);
}
</script>
</head>
<body>
<p id="pNode">Foo</p>
<div id="divNode">Bar</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

是否有可能修改它以便pNode保留在身体下面以及divNode?如果节点的两个父节点没有意义,那么是否可以创建新副本pNode并将此新副本附加到divNode

html javascript dom

3
推荐指数
1
解决办法
844
查看次数

使用JavaScript访问时,DOM元素的style属性为空字符串

我为ID为#foo的元素定义了以下样式.

#foo {
    display: none;
}
Run Code Online (Sandbox Code Playgroud)

我运行以下JavaScript代码:

var foo = document.getElementById('foo')
alert('[' + foo.style.display + ']')
Run Code Online (Sandbox Code Playgroud)

我期待输出[none].但是,输出很简单[].这是一个演示:http://jsfiddle.net/bEmUE/

为什么foo.style.display在这里是一个空字符串?

我可以在代码中编写什么来计算这个元素的实际显示属性?

html javascript dom

3
推荐指数
1
解决办法
1598
查看次数

JavaScript focus()函数不会在页面加载时将textarea聚焦

我有以下代码.

<!DOCTYPE html>
<html>
<head>
<title>Focus issue</title>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
  var text = document.getElementById('text')
  window.onload = function() {
    text.focus()
  }
  window.onhashchange = function() {
    text.focus()
  }
}//]]>  
</script>
</head>
<body>
  <textarea id="text"></textarea>
  <p><a href="#focus">Click to focus</a></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是上面代码的JSFiddle演示:http://jsfiddle.net/DvU63/

为什么页面加载时焦点不会转到textarea框?在单击链接时,焦点确实在textarea框上,但我也希望焦点转到页面加载时的textarea框.为什么不发生?

注意:我知道textarea的HTML5自动聚焦属性.但是这个问题是为什么上面的JavaScript代码没有做我想做的事情.

html javascript html5

3
推荐指数
1
解决办法
2032
查看次数

如何在我的nginx + uwsgi环境中使用environ ['PATH_INFO']?

我在/www/app.py中写了一个简单的瓶子应用程序.

import bottle

app = bottle.Bottle()
@app.route('/')
def index():
    return 'hello from bottle'

application=app
Run Code Online (Sandbox Code Playgroud)

我已将我的nginx虚拟主机配置在名为/ etc/nginx/sites-enabled/foo的文件中:

server {
    listen 8080;
    root /www;
    index index.html index.htm;
    server_name foo;
    location / {
        uwsgi_pass 127.0.0.1:9090;
    }
}
Run Code Online (Sandbox Code Playgroud)

我以这种方式启动nginx和uwsgi:

service nginx restart
uwsgi --socket 127.0.0.1:9090 --wsgi-file app.py
Run Code Online (Sandbox Code Playgroud)

但是当我尝试访问时,我http://foo/在网页中收到此错误:

Critical error while processing request: /
Run Code Online (Sandbox Code Playgroud)

我在uwsgi输出中得到这个错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.3/dist-packages/bottle.py", line 954, in wsgi
    out = self._cast(self._handle(environ))
  File "/usr/local/lib/python3.3/dist-packages/bottle.py", line 845, in _handle
    path = environ['bottle.raw_path'] = environ['PATH_INFO']
KeyError: 'PATH_INFO' …
Run Code Online (Sandbox Code Playgroud)

python nginx bottle

3
推荐指数
1
解决办法
2098
查看次数

Vim 中的 x 命令如何处理在可视模式下选择的文本,即使它不是运算符?

引用中提到的运算符列表:h operator

The motion commands can be used after an operator command, to have the command
operate on the text that was moved over.  That is the text between the cursor
position before and after the motion.  Operators are generally used to delete
or change text.  The following operators are available:

    |c| c   change
    |d| d   delete
    |y| y   yank into register (does not change the text)
    |~| ~   swap case (only if 'tildeop' is set)
    |g~|    g~ …
Run Code Online (Sandbox Code Playgroud)

vim

3
推荐指数
1
解决办法
349
查看次数

我们可以删除C宏定义中参数周围的括号吗?

http://c-faq.com/style/strcmp.html,我学到了以下便利宏:

#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)
Run Code Online (Sandbox Code Playgroud)

我想知道为什么在这个宏中使用了这么多括号.每个括号是用于某个目的还是使用冗余括号的宏,这些括号没有用处?

我们可以删除周围的括号s1s2像这样制作一个宏吗?

#define MyStreq(s1, s2) (strcmp(s1, s2) == 0)
Run Code Online (Sandbox Code Playgroud)

这个MyStreq宏似乎对我很有用Streq.

#include <string.h>
#include <stdio.h>

#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)
#define MyStreq(s1, s2) (strcmp(s1, s2) == 0)

int main()
{
    printf("%d %d\n", Streq("foo", "foo"), MyStreq("foo", "foo"));
    printf("%d %d\n", Streq("fox", "foo"), MyStreq("fox", "foo"));
    printf("%d %d\n", Streq("foo", "fox"), MyStreq("foo", "fox"));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面代码的输出:

1 1
0 0
0 0
Run Code Online (Sandbox Code Playgroud)

你能想象这些宏的使用在哪里Streq可以实现人们所期望的但却 …

c macros parentheses c-preprocessor

3
推荐指数
2
解决办法
922
查看次数

为什么编译器会抱怨对constexpr函数的未定义引用,即使它是在另一个源文件中定义的?

我有两个文件的源代码.

第一个文件包含int main()函数的功能和声明以及用法constexpr int square(int x).

// File: foo.cpp
#include <iostream>

constexpr int square(int x);

int main()
{
    int a = square(10);
    std::cout << "a: " << a << "\n";
}
Run Code Online (Sandbox Code Playgroud)

第二个文件包含constexpr int square(int x)函数的定义.

// File: bar.cpp
constexpr int square(int x)
{
    return x * x;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译这两个文件时,我收到以下错误.

$ g++ -std=c++11 bar.cpp foo.cpp
foo.cpp:4:15: warning: inline function ‘constexpr int square(int)’ used but never defined
 constexpr int square(int x);
               ^
/tmp/cc7iwVDZ.o: In function `main': …
Run Code Online (Sandbox Code Playgroud)

c++ const c++11

3
推荐指数
1
解决办法
224
查看次数

Mocha如何知道异步测试中的测试失败?

我试图了解Mocha的异步代码(http://mochajs.org/#getting-started)是如何工作的.

describe('User', function() {
  describe('#save()', function() {
    it('should save without error', function(done) {
      var user = new User('Luna');
      user.save(function(err) {
        if (err) throw err;
        done();
      });
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

我想知道Mocha如何决定测试在幕后是成功还是失败.

从上面的代码我可以理解,user.save()异步会立即返回.所以Mocha在执行之后不会决定测试是成功还是失败it().当user.save()最终done()成功调用时,摩卡会认为这是一次成功的测试.

我无法理解摩卡在上述情况下如何了解测试失败.比方说,user.save()err参数集调用它的回调,然后回调引发错误.在这种情况下,没有调用Mocha的函数.那么摩卡怎么会知道回调中发生了错误?

javascript mocha.js

3
推荐指数
1
解决办法
778
查看次数

为什么当表达式中有另一个长值时,我的整数文字没有被提升为long类型?

码:

public class Foo
{
    public static void main(String[] args)
    {
        long i = 4294967296l;
        System.out.println(i + 65536 * 65536);
        System.out.println(i + 65536L * 65536);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

4294967296
8589934592
Run Code Online (Sandbox Code Playgroud)

它看起来像在第一个System.out.println语句中,65536 * 65536被评估为int它包装的结果类型0.

我想知道为什么在这个声明中,数字没有提升到long.我认为i这个陈述中长变量的存在足以促进65536长期变化.

java types

3
推荐指数
1
解决办法
72
查看次数

标签 统计

javascript ×4

html ×3

dom ×2

bottle ×1

c ×1

c++ ×1

c++11 ×1

c-preprocessor ×1

const ×1

go ×1

grammar ×1

html5 ×1

java ×1

macros ×1

mocha.js ×1

nginx ×1

parentheses ×1

python ×1

semantics ×1

syntax ×1

types ×1

vim ×1