考虑以下示例:
function previewFile(file) {
var reader = new FileReader();
reader.onloadend = function () {
console.log(reader.result);
}
reader.readAsDataURL(file);
}
Run Code Online (Sandbox Code Playgroud)
它指出:
instanceOfFileReader.readAsDataURL(BLOB);
blob:要从中读取的Blob或文件.
如何将本地文件URL 'file:///C:/path-to/root.png'
传递给readAsDataURL()
是否FileReader()
可以在Firefox的附加组件?
我试图bind
(即bind(this)
)内联匿名回调函数object
如何做到?
简化示例:
var object = {
property: function() {
this.id = 'abc'; // 'this' binds to the object
aFunctionWithCallback(this.id, function(data) {
console.log(this); // null
});
}
};
Run Code Online (Sandbox Code Playgroud) 我试图将数组元素设置为对象属性
简化示例:
var array = ['a', 'b', 'c'];
var obj = { array[1]: 'good' }
Run Code Online (Sandbox Code Playgroud)
上面会导致错误.
更新:事实上,我将对象作为另一个数组的一部分传递,即一个简化的例子是:
aObj[value] = ['one', {array[1]: 'good'}, 'two', 'three', 'four'];
Run Code Online (Sandbox Code Playgroud)
设置obj[array[1]] = 'good';
样式意味着使用
aObj[value][1][array[1]] = 'good';
Run Code Online (Sandbox Code Playgroud) 已经有一些关于class
OOP 是各种语言的问答。
看起来该static
方法比 Instance 方法略快,但在常见的实际使用中差异不大。
参考:
使用静态方法与实例化包含方法的类的性能
速度测试:静态与实例方法
在类实例示例中,创建了 2 个对象,原始对象class
和克隆对象也使用new
.
(如果存在多个不同数据的进程需要设置值并同时使用同一个类的情况,那么new
为每个进程创建一个克隆将保持数据完整性。)
在static
示例中,只创建一个对象。
在这两种类型之间做出决定时,是否需要考虑任何好处?
例如:
// class instance
class Triple {
do(n = 1) {
return n * 3;
}
}
const triple = new Triple();
triple.do(5); // 15
// static method
class Triple {
static do(n = 1) {
return n * 3;
}
}
Triple.do(5); // 15
Run Code Online (Sandbox Code Playgroud)
更新 constructor
问题
似乎 constructor
只有在class
实例中才有效。
参考: …
处理 时searchParams
,=
为空值添加的额外内容会导致错误的 URL。加工时
有没有办法防止不必要的情况发生?=
searchParams
注意:可以删除额外=
的RegExp()
,但问题的目的是使用 来实现它searchParams
。
const u = new URL('https://example.com/?4FEE63D94&foo=1');
params = u.searchParams;
params.delete('foo'); // Delete the foo parameter
console.log(u.href); // https://example.com/?4FEE63D94=
Run Code Online (Sandbox Code Playgroud)
URLSearchParams方法似乎会改变 URL。
URLSearchParams.entries()
&&URLSearchParams.forEach()
替换URLSearchParams.get()
为+
空格 ( %20
)。
const u = new URL('https://example.com/?4FEE63D94&foo=1&bar=aaa+bbb+ccc');
let params = u.searchParams;
params.delete('foo'); // Delete the foo parameter
console.log(u.href); // https://example.com/?4FEE63D94=&bar=aaa+bbb+ccc
u.search = [...params.entries()].map(([key, value]) => value ? `${key}=${value}` : key).join('&');
console.log(u.href); // …
Run Code Online (Sandbox Code Playgroud)自从几天前和最新的 VS Code 更新以来,VS Code 启动时会出现解锁登录密钥环的提示(以前从未发生过)。
如何禁用提示?
VS code 在单用户笔记本电脑上独立使用,无需 GIT 或共享目录,也无需使用 npm。请注意,VS Code 已经在同一台机器上以相同的设置运行了 1.5 年,但提示只是在几天前才开始。
Ubuntu 23.04 Wayland
版本:1.80.0
操作系统:Linux x64 6.2.0-24-generic snap
附言。几天前,我点击 VS Code 打开的文件中的 URL 在 Firefox 中打开。这会导致变化吗?
我想知道是否有可能拦截和控制/重定向Firefox发出的DNS请求?
目的是在Firefox中设置一个独立的DNS服务器(而不是系统的DNS服务器)
是否可以仅使用 CSS 来制作 HTML5 进度元素的动画?我知道怎么用JS来做。我想知道是否仅使用 CSS 就可以实现。
<progress value="0" max="100"></progress>
Run Code Online (Sandbox Code Playgroud) 如何将本地(文件系统)URI转换为路径?
它可以用nsIIOService
+ newURI()
+ QueryInterface(Components.interfaces.nsIFileURL)
+ 完成,file.path
但这看起来很长.
有更短的方式吗?
这是一个示例代码:
var aFileURL = 'file:///C:/path-to-local-file/root.png';
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var url = ios.newURI(aFileURL, null, null); // url is a nsIURI
// file is a nsIFile
var file = url.QueryInterface(Components.interfaces.nsIFileURL).file;
console.log(file.path); // "C:\path-to-local-file\root.png"
Run Code Online (Sandbox Code Playgroud)