小编Seb*_*tel的帖子

如何知道D3.js中的当前缩放级别

几天后我有一个问题.我有一个图表,当我使用缩放行为它工作,但我需要知道何时达到最大缩放以加载新的给定

// Specifies the zoom scale's allowed range, [min, max]
d3.behavior.zoom().x(x).scaleExtent([1,10]).on("zoom", draw)
Run Code Online (Sandbox Code Playgroud)

看我的代码 http://jsfiddle.net/albanlopez/D4MRP/

javascript events graph zoom d3.js

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

未显示警报前的HTML

在Chrome,下面的代码不显示<h1>Hello</h1>直到之后alert示出和用户单击OK.但是,在Firefox中会<h1>Hello</h1>显示预期的序列,然后显示警报.

<h1>Hello</h1>
<script>
  alert('Hello is displayed after this alert')
</script>
Run Code Online (Sandbox Code Playgroud)

我很好奇为什么Hello在Chrome中关闭警报后显示.这是应该发生的吗?HTML/JavaScript规范是不是很清楚,Chrome只是从不打扰这个直观吗?这是Chrome中的错误吗?

html javascript alert dom google-chrome

9
推荐指数
1
解决办法
89
查看次数

TensorFlowJS 中的单位和 inputShape

我是 TensorflowJS 和 ML 的新手。在API参考中,有以下代码。

const model = tf.sequential();

// First layer must have an input shape defined.
model.add(tf.layers.dense({units: 32, inputShape: [50]}));

// Afterwards, TF.js does automatic shape inference.
model.add(tf.layers.dense({units: 4}));

// Inspect the inferred shape of the model's output, which equals
// `[null, 4]`. The 1st dimension is the undetermined batch dimension; the
// 2nd is the output size of the model's last layer.
console.log(JSON.stringify(model.outputs[0].shape));
Run Code Online (Sandbox Code Playgroud)

我想知道的是,

什么是inputShape

自动形状是什么?

既然unit提到了数据集的属性,为什么unit要设置为4呢model.add(tf.layers.dense({units: 4})) …

javascript tensorflow tensorflow.js

5
推荐指数
1
解决办法
2733
查看次数

为什么array.forEach(()=> {array.pop()})不会清空数组

在nodejs REPL上,我试图清理一个定义为的数组,const array = [...] 并且发现使用array.forEach(() => /pop|shift/())不起作用.在这样的表达式之后,数组仍将保留其中的值.

我很清楚更好的清理阵列的方法,比如array.splice(0),但我对这种行为很好奇,似乎反直觉,至少对我而言.

这是测试:

const a = [1, 2, 3]

a.forEach(() => {
  a.shift()
})

console.log(a) // [ 3 ]

const b = [1, 2, 3]

b.forEach(() => {
  b.pop()
})

console.log(b) // prints [ 1 ]
Run Code Online (Sandbox Code Playgroud)


笔记

  1. 起初我正在使用arr.forEach(() => arr.pop()),所以我虽然其中一个值是短路forEach但是将lambda包裹在一个体块中{ .. }也会产生相同的结果.

  2. 结果在不同的节点版本和浏览器中是一致的.所以它似乎是明确定义的行为.

  3. 剩余值的数量,仍在结果数组中的值,根据输入数组的长度而变化,似乎是 Math.floor(array.length / 2)

  4. 剩余的值总是根据使用的/pop|shift/方法排序,因此一些调用实际上是在改变输入数组.

  5. 它也通过调用返回相同的结果 Array.prototype.forEach(array, fn)

javascript arrays node.js

5
推荐指数
1
解决办法
93
查看次数

PHP“??” JavaScript 中的运算符

简单的问题。javascript中是否存在类似php的东西??

在 php 中你可以这样做condition ?? statement2

这与 相同condition? condition : statement2

javascript中有类似的东西吗?

javascript

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

第一次在对象内调用时,JS对象属性未定义

考虑以下JS对象,它具有:

  • x:变量给定值100
  • p:打印x的值,在创建后立即调用
  • y:打印x的值,在创建对象后调用

当在对象创建期间立即打印Pipe.x的值时,由于某种原因,Pipe.x是,undefined但是当调用Py()时,在对象之后创建了Object,Pipe.x的值为100,因为它应该是第一位的.

var Pipe = {
    x: 100,

    p: function(){
        console.log('p says x is ' + this.x); // prints y says x is undefined
    }(),    // call immediately

    y: function(){
        console.log('y says x is ' + this.x);
    }
}

Pipe.y();    // prints y says x is 100
Run Code Online (Sandbox Code Playgroud)

是否有任何JS对象属性我缺少使得Pipe.p()打印undefined和Py()打印100?

javascript node.js javascript-objects

2
推荐指数
1
解决办法
61
查看次数

将 HTML DOM 元素转换为 JavaScript 对象?

我有一个包含元素的网页:

<div id='myid'></div>
Run Code Online (Sandbox Code Playgroud)

在 chrome 开发工具中,当我在控制台中输入时

document.getElementById('myid')
Run Code Online (Sandbox Code Playgroud)

返回一个 html 元素,

<div id='myid'></div>
Run Code Online (Sandbox Code Playgroud)

有没有我可以使用的替代方法来查看它,它的属性作为 JavaScript 对象而不是 html 元素?

将 html 转换为 javascript

我看过这篇文章,但我想要做的就是获取元素,而不是做任何解析并构建一些新的东西,最好是在开发工具中。

html javascript object

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

将\n转换为<br/>

我在将\n转换为<br />时遇到问题

function format() {
  var text = document.getElementById("formatArea").value;
  text.replace("\n", "<br/>");
  document.getElementById("resultArea").value = text;
}
Run Code Online (Sandbox Code Playgroud)
<textarea rows="20" cols="80" id="formatArea">
</textarea>


<textarea rows="20" cols="80" id="resultArea">
</textarea>

<button onclick="format()">Click to create HTML breaks</button>
Run Code Online (Sandbox Code Playgroud)

感谢任何帮助,我在JS方面还不是很有经验.

html javascript

-1
推荐指数
1
解决办法
128
查看次数