我有一个myModule Node.js模块,其中包含:
function b() {
console.log('original b');
}
function a() {
b();
}
exports.a = a
exports.b = b;
Run Code Online (Sandbox Code Playgroud)
以及以下使用mocha + sinon.js的测试套件:
const myModule = require('./myModule.js');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(sinonChai);
describe('not working stub', () => {
it('should call the stub', () => {
let stub = sinon.stub(myModule, 'b', () => { console.log('stubbed b')});
myModule.a();
expect(stub).to.have.been.called;
})
});
Run Code Online (Sandbox Code Playgroud)
我期望存根被调用,但是原来的b被调用了,为什么?
我正在尝试将使用 emscripten 生成的模块导入为 es6 模块。我正在尝试使用emscripten doc 中的基本示例。
这是我用来从 C 模块生成 js 模块的命令:
emcc example.cpp -o example.js -s EXPORTED_FUNCTIONS="['_int_sqrt']" -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" -s EXPORT_ES6=1 -s MODULARIZE=1
C 模块:
#include <math.h>
extern "C" {
int int_sqrt(int x) {
return sqrt(x);
}
}
Run Code Online (Sandbox Code Playgroud)
然后导入生成的js模块:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Wasm example</title>
</head>
<body>
<script type="module">
import Module from './example.js'
int_sqrt = Module.cwrap('int_sqrt', 'number', ['number']);
console.log(int_sqrt(64));
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是失败的,因为 cwrap 在 Module 对象上不可用:
Uncaught TypeError: Module.cwrap is not …
我试图了解Chrome和Firefox之间的行为差异.我已经发布了以下CodePen来说明这一点.
使用Firefox时,页脚元素会粘贴到包装div的底部,这要归功于margin-top: -50px它的应用.这是我所期待的.
使用Chrome(和IE),margin-bottomp元素会向下推动页脚,使其溢出.这是为什么?
html, body {
margin: 0px;
height: 100%
}
header {
background-color: dodgerblue;
opacity: 0.5;
}
#wrapper {
background-color: tomato;
position: relative;
min-height: 100%;
}
#container p {
background-color: lightgreen;
width: 500px;
/*margin-bottom: 0px;*/
}
footer {
height: 50px;
background-color: dodgerblue;
margin-top: -50px;
opacity: 0.5;
}Run Code Online (Sandbox Code Playgroud)
<body>
<div id="wrapper">
<header>.</header>
<div id="container">
<p>.</p>
</div>
</div>
<footer></footer>
</body>Run Code Online (Sandbox Code Playgroud)
我已经在纯 Javascript 和 C 中实现了一个非常简单的快速排序,后者被导出为 WebAssembly 模块。
我正在对[0;1000] 范围内的 2 个相同的 10 6 个整数数组进行排序。纯 javascript 实现平均需要 780 毫秒,而基于 WebAssembly 的实现需要 935 毫秒(未设置优化标志时为 1020 毫秒)。
为什么纯 javascript 实现最快?
在有问题的 2 个实现下面:
JS:
function swap(array, swapy1, swapy2) {
let tmp = array[swapy1];
array[swapy1] = array[swapy2];
array[swapy2] = tmp;
}
function partition(array, lo, hi) {
const pivot = array[hi];
let i = lo - 1;
for (let j = lo; j < hi; j++) {
if (array[j] < pivot) {
if (i …Run Code Online (Sandbox Code Playgroud) 与我合作的后端团队实现了一个 REST API,它将集合作为对象(而不是数组)返回。例如,调用GET /some_resources将返回:
{
id_1: {
p1: "lorem",
p2: 4
},
id_2: {
p1: "ipsum",
p2: 2
},
id_n: {
p1: "sic",
p2: 7
}
}
Run Code Online (Sandbox Code Playgroud)
这种结构仅限于在 Angular 中使用,因为例如您不能利用ng-repeat指令(例如orderBy 和 filter 不适用于 object)。
对于前面的例子,理想情况下,我宁愿期望一个对象数组:
[
{id: "id_1", p1: "lorem", p2: 4},
{id: "id_2", p1: "ipsum", p2: 2},
{id: "id_n", p1: "sic", p2: 7}
]
Run Code Online (Sandbox Code Playgroud)
这将允许直接使用ngResource服务返回的对象并将其发送回来,而无需触及其结构。它还允许完全使用ng-repeat指令。
当我要求后端团队将他们的集合响应结构从对象更改为数组时,他们认为 API 使用者不应主导响应结构的设计。
我部分同意他们的论点,所以现在,我找到的唯一解决方案是将对象转换为数组,将此对象处理到我的作用域中,然后将数组重新转换回原始对象结构,然后再将其发送回 API .
这导致了很多代码样板,我想知道是否有一种“角度方式”来处理这种情况。
我正在尝试在我的本地apache上运行以下示例http://mrdoob.github.com/three.js/examples/webgl_geometry_cube.html,我只需替换以下行:
var texture = THREE.ImageUtils.loadTexture('textures/crate.gif');
Run Code Online (Sandbox Code Playgroud)
通过
var texture = THREE.ImageUtils.loadTexture('http://mrdoob.github.com/three.js/examples/textures/crate.gif');
Run Code Online (Sandbox Code Playgroud)
我在我启用的站点配置中添加了允许跨域请求的指令,如下所示:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName localhost
DocumentRoot /var/www
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
<Directory /var/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn …Run Code Online (Sandbox Code Playgroud) javascript ×3
emscripten ×2
webassembly ×2
angularjs ×1
apache ×1
cors ×1
css ×1
es6-modules ×1
node.js ×1
rest ×1
sinon ×1
textures ×1
three.js ×1