在努力提高渐进式网络应用程序的性能的同时,我遇到了一个新功能Passive Event Listeners,我发现很难理解这个概念.
什么是Passive Event Listeners,什么是需要有它在我们的项目?
javascript google-chrome event-listener dom-events passive-event-listeners
在javascript中编写速记if-else时,会出现语法错误.这是我的代码:
data && data.cod == '404' && return;
Run Code Online (Sandbox Code Playgroud)
虽然我使用正常的if-else工作正常如下:
if(data && data.cod == '404') {return};
var temp = data && data.main && data.main.temp;
//Code here...
Run Code Online (Sandbox Code Playgroud)
我知道,如果我使用三元运算符,它的工作正常,return (data && data.cod == '404')?'true':'false';但我在条件基础上看"返回",否则继续进行.
像这样的代码:
var str = "Hello StackOverflow !";
alert(typeof str);
Run Code Online (Sandbox Code Playgroud)
给我string结果.这意味着字符串不是对象,那么为什么我们有一个字符串的性质str一样str.substring,str.indexOf etc.?
同样当我设置属性,以它作为
str.property = "custom property is set";并试图得到这个alert(str.property),它给了我undefined.为什么?
我写了下面的代码来检查特定的 URL 是否已经在 service worker 缓存中?但即使 URL 不存在于缓存中,它也会返回/控制台“在缓存中找到”。
var isExistInCache = function(request){
return caches.open(this.cacheName).then(function(cache) {
return cache.match(request).then(function(response){
debug_("Found in cache "+response,debug);
return true;
},function(err){
debug_("Not found in cache "+response,debug);
return false;
});
})
}
Run Code Online (Sandbox Code Playgroud)
调用上面的函数作为
cache.isExistInCache('http://localhost:8080/myroom.css').then(function(isExist){
console.log(isExist);
})
Run Code Online (Sandbox Code Playgroud) 因为我正在构建渐进式Web应用程序.我们面临服务工作者的奇怪行为.
1. Clear cache and unregister service worker
2. Go to www.example.com
3. Examine the network calls for resources (JS/CSS)
Run Code Online (Sandbox Code Playgroud)
预期结果:只有一个网络请求应该用于一个资源.
实际结果:正在为每个资源发出两个网络请求[![SCREENSHOT [1]](https://i.stack.imgur.com/cVkkE.jpg)