Questions tagged «javascript»

有关在ECMAScript(JavaScript / JS)及其各种方言/实现(不包括ActionScript)中进行编程的问题。此标记很少单独使用,但最常与标记[node.js],[jquery],[json]和[html]关联。

1
在Create-React-App应用程序中index.html和index.js之间的连接在哪里?
我开始玩Create React App,但我不明白内如何index.js加载index.html。这是html代码: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> <!-- Notice the use of %PUBLIC_URL% in the tag above. It will be replaced with the URL of the `public` folder during the build. Only files inside the `public` folder can be …

1
如何判断浏览器是否支持<input type ='date'>
可能重复: HTML5类型检测和插件初始化 &lt;input type=date&gt; 应该使用(可选的)用户代理提供的datepicker创建输入,但是尚未得到广泛支持,因此我们使用的是jQuery UI Datepicker。我们如何允许浏览器使用自己的日期选择器,并且仅当浏览器没有这种东西时才使用jQuery UI? 目前,我认为只有Opera内置了日期选择器,但是对Opera的测试显然是不好的。有没有一种方法可以检测到此功能(如果可以通过便携式方式检测到)?
82 javascript  html 

9
用JavaScript创建Set的方法?
在Eloquent JavaScript,第4章中,通过创建对象并将值存储为属性名称,然后将任意值(例如true)分配为属性值来创建一组值。要检查该值是否已包含在集合中,请使用in运算符: var set = {}; if (!'Tom' in set) { set.Tom = true; } 这是惯用的JavaScript吗?会不会使用更好的数组? var set = []; if (!'Tom' in set) { set.push = 'Tom'; }
82 javascript  set 

5
在JavaScript中的另一个函数中定义一个函数
function foo(a) { if (/* Some condition */) { // perform task 1 // perform task 3 } else { // perform task 2 // perform task 3 } } 我有一个函数,其结构与上述类似。我想将任务3抽象为一个函数,bar()但是我希望将此函数的访问限制在的范围内foo(a)。 为了实现我想要的,更改以下内容是否正确? function foo(a) { function bar() { // Perform task 3 } if (/* Some condition */) { // …
82 javascript 

5
offsetTop与jQuery.offset()。top
我已经阅读过,offsetLeft并且offsetTop不能在所有浏览器中正常工作。 jQuery.offset()应当为此提供一个抽象,以提供正确的值xbrowser。 我想做的是获取相对于元素左上角单击元素的位置的坐标。 问题是jQuery.offset().top实际上是在FFX 3.6中给了我一个十进制值(在IE和Chrome中,这两个值匹配)。 这个小提琴展示了这个问题。如果单击底部图像,则jQuery.offset().top返回327.5,但offsetTop返回328。 我想认为这offset()是返回正确的值,我应该使用它,因为它可以在所有浏览器中使用。但是,人们显然不能单击像素的小数位。是确定Math.round()jQuery返回的偏移量的真实偏移量的正确方法吗?我应该使用offsetTop替代方法还是完全使用其他方法?


5
Math.max.apply()如何工作?
Math.max.apply()工作如何? &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset=utf-8 /&gt; &lt;title&gt;JS Bin&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;script&gt; var list = ["12","23","100","34","56", "9","233"]; console.log(Math.max.apply(Math,list)); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/max 上面的代码在列表中找到最大数。谁能告诉我下面的代码如何工作?如果我通过的话似乎可行null or Math. console.log(Math.max.apply(Math,list)); 是否所有user-defined/Native functions我们都可以使用的调用和应用方法?

13
Socket.io客户端:用一个处理程序响应所有事件?
是否有可能让socket.io客户端响应所有事件而无需单独指定每个事件? 例如,如下所示(显然现在不起作用): var socket = io.connect("http://myserver"); socket.on("*", function(){ // listen to any and all events that are emitted from the // socket.io back-end server, and handle them here. // is this possible? how can i do this? }); 我希望在客户端socket.io代码收到任何/所有事件时调用此回调函数。 这可能吗?怎么样?

1
有人可以解释Webpack的CommonsChunkPlugin
我得到了一个基本要点,即CommonsChunkPlugin查看所有入口点,检查它们之间是否存在通用的软件包/依赖项,并将它们分成自己的捆绑包。 因此,假设我具有以下配置: ... enrty : { entry1 : 'entry1.js', //which has 'jquery' as a dependency entry2 : 'entry2.js', //which has 'jquery as a dependency vendors : [ 'jquery', 'some_jquery_plugin' //which has 'jquery' as a dependency ] }, output: { path: PATHS.build, filename: '[name].bundle.js' } ... 如果我捆绑而不使用 CommonsChunkPlugin 我将得到3个新的捆绑包文件: entry1.bundle.js包含来自entry1.js和的完整代码,jquery并包含自己的运行时 entry2.bundle.js包含来自entry2.js和的完整代码,jquery并包含自己的运行时 …


6
Node.js捕获生成后引发的ENOMEM错误
使用spawn时,由于抛出ENOMEM(内存不足)errnoException,我的Node.js脚本崩溃了。 错误: child_process.js:935 throw errnoException(process._errno, 'spawn'); ^ Error: spawn ENOMEM at errnoException (child_process.js:988:11) at ChildProcess.spawn (child_process.js:935:11) at Object.exports.spawn (child_process.js:723:9) at module.exports ([...]/node_modules/zbarimg/index.js:19:23) 我已经在为errorandexit事件使用侦听器,但是在发生此错误的情况下不会触发它们。 我的代码: zbarimg = process.spawn('zbarimg', [photo, '-q']); zbarimg.on('error', function(err) { ... }); zbarimg.on('close', function(code) { ... }); 完整的源代码可用。 我有什么办法可以防止脚本崩溃?如何捕获抛出的ENOMEM错误? 谢谢!

4
在JSDoc中记录开放式参数函数的正确方法
假设您有类似以下内容: var someFunc = function() { // do something here with arguments } 您如何正确记录该函数可以在JSDoc中接受任意数量的参数?这是我的最佳猜测,但我不确定这是正确的。 /** * @param {Mixed} [...] Unlimited amount of optional parameters */ var someFunc = function() { // do something here with arguments } 相关文档:php-如何记录可变数量的参数
82 javascript  jsdoc 

6
生成器的异步/等待和ES6收益之间的区别
我刚刚读了这篇很棒的文章《生成器》,它清楚地突出了此函数,它是处理生成器函数的辅助函数: function async(makeGenerator){ return function () { var generator = makeGenerator.apply(this, arguments); function handle(result){ // result =&gt; { done: [Boolean], value: [Object] } if (result.done) return Promise.resolve(result.value); return Promise.resolve(result.value).then(function (res){ return handle(generator.next(res)); }, function (err){ return handle(generator.throw(err)); }); } try { return handle(generator.next()); } catch (ex) { return Promise.reject(ex); } …


7
用“,”分隔句子并删除周围的空格
我有这个代码: var r = /(?:^\s*([^\s]*)\s*)(?:,\s*([^\s]*)\s*){0,}$/ var s = " a , b , c " var m = s.match(r) m =&gt; [" a , b , c ", "a", "c"] 看起来整个字符串都已匹配,但是哪里"b"去了?我希望得到: [" a , b , c ", "a", "b", "c"] 这样我就可以m.shift()处理类似的结果,s.split(',')但也可以删除空格。 我在正则表达式中有错误还是误解String.prototype.match?
82 javascript  regex 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.