任何有 DeviceOrientationEvent 经验和手机/平板电脑的人吗?
在带有陀螺仪的设备上运行以下代码片段,我注意到 gamma(沿 y 轴向左/向右旋转)随着 beta 接近 90 度(设备指向正上方)变大且不可预测。我假设这是gimbal-lock。
var data, raf, alpha = document.getElementById("alpha"),
beta = document.getElementById("beta"),
gamma = document.getElementById("gamma");
window.addEventListener("deviceorientation", processData);
window.addEventListener('click', togglePause);
updateText();
function processData(e) {
data = e;
}
function updateText() {
if (data) {
alpha.textContent = Math.round(data.alpha);
beta.textContent = Math.round(data.beta);
gamma.textContent = Math.round(data.gamma);
}
raf = requestAnimationFrame(updateText);
}
function togglePause(e) {
if (raf) {
cancelAnimationFrame(raf);
raf = null;
window.removeEventListener("deviceorientation", processData);
} else {
window.addEventListener("deviceorientation", processData);
raf = requestAnimationFrame(updateText);
}
}Run Code Online (Sandbox Code Playgroud)
body …Run Code Online (Sandbox Code Playgroud)给定以下程序,控制台正确记录 - 注意链接init功能和return this:
const cat = {
init(sound) {
this.sound = sound;
return this;
},
makeSound() {
console.log(this.sound);
}
};
const fluffy = Object.create(cat).init('meeeaaaauuu');
fluffy.makeSound();Run Code Online (Sandbox Code Playgroud)
我的问题:它是如何以及为什么return this需要它起作用? 请参阅下面的错误,删除它:
const cat = {
init(sound) {
this.sound = sound;
// return this
},
makeSound() {
console.log(this.sound);
}
};
const fluffy = Object.create(cat).init('meeeaaaahuuu');
fluffy.makeSound();Run Code Online (Sandbox Code Playgroud)
MDN声明Object.create返回新对象,因此链接init()应该工作......通过思考...... 是因为链接的新对象仍然是"匿名"的吗?
请注意,如果init()获得自己的行,所有工作都如我所料,不需要return this:
const fluffy = Object.create(cat);
fluffy.init('meeeaaaahuuu');
fluffy.makeSound();
Run Code Online (Sandbox Code Playgroud) 是否有可能将外部函数从其内部函数中分离出来?
(function one(){
var foo = (function two(){
if(true){
return true; //all good
}else{
//how can we break function one here?
})();
//extra statements only executed if foo is true
})();
Run Code Online (Sandbox Code Playgroud)
没有所谓的“退货”吧?:P
更新
谢谢大家 - 因为我怀疑我正在寻找的“退货”功能是不可能的。但是根据您的输入/提示,我重新编写了代码并使用了 try/catch 块。在 JS 中给猫剥皮的方法有很多种!
除语义和浏览器支持外,原始值属性和HTML5数据属性之间是否有任何区别?
<div id="mydiv" value="myvalue" data-somedata="mydata"></div>
Run Code Online (Sandbox Code Playgroud)