当JS代码开始运行时,全局执行上下文被创建并位于执行堆栈的底部,以"容纳"全局变量对象和'this'.如果在执行整个JS代码并且没有全局执行上下文后执行堆栈变空,那么我们如何仍然能够访问全局变量(例如,我正在运行带有JS的html文件)代码和完成后,我仍然能够通过Chrome控制台看到全局变量的值......)或者如何this仍然指向全局对象('this'如果没有任何执行上下文,则不应该有任何内容!)?
我可能给自己的唯一解释是全局执行上下文永远不会离开执行堆栈; 它一直存在,直到我决定关闭浏览器窗口.我是对还是不对?
此外,在异步回调的情况下,当一个事件离开事件队列并进入JS引擎运行时,执行堆栈中究竟发生了什么?回调的执行上下文是否位于此堆栈的底部,还是全局执行上下文仍然存在?
有一个类似的主题是初始全局执行上下文是否从JavaScript调用堆栈弹出?; 但是,它没有回答我的问题.
谢谢
<input name="e_password" id="e_password" type="password" autocomplete="off" />
Run Code Online (Sandbox Code Playgroud)
上面是一个密码字段,由于某种原因,仅在 Mozilla 中自动填充(而不是在 Chrome 和 IE11 中)。显然,它保留了之前试验的值字段。我做了很多尝试来清除这个领域。我可以管理它的唯一方法是通过以下方式:
$(document).ready(function(){
$('input:text').val('');
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,上面的内容重置了表单内的所有其他文本字段,这是不希望的。还,
$(document).ready(function(){
$('input:password').val('');
});
Run Code Online (Sandbox Code Playgroud)
不起作用!此外,我尝试了这个:
<div class="abc">
<input name="e_password" id="e_password" type="password" autocomplete="off" />
</div>
$(document).ready(function(){
$('.abc input:text').val('');
});
Run Code Online (Sandbox Code Playgroud)
也不行...当然,我尝试了显而易见的方法:
$('#e_password').val('');
Run Code Online (Sandbox Code Playgroud)
这也不起作用。我再说一遍,这个问题只存在于 Mozilla 中。我还能做什么?
谢谢
请考虑以下代码:
<head>
<style>
.box{
background-color:red;
height:150px;
width:150px;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
var start = new Date();
while(true) {
var now = new Date();
if (now-start > 10000)
break;
}
console.log('main thread finished');
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,DOM将其加载延迟了十秒钟(10秒后出现.box矩形!).既然它是第一个( <div class="box"></div> ),为什么要等待下面的脚本?任何合理的解释?
谢谢
export class ApplyComponent implements OnInit {
formApply: FormGroup;
postCodeInput: boolean = true;
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.formApply = this.fb.group({
firstName: ["", Validators.required],
currentUKAddress: this.fb.group({
flat: ["", Validators.required],
years: ["", Validators.required]
})
});
this.onChanges();
}
onChanges(): void {
...
}
Run Code Online (Sandbox Code Playgroud)
我想听听岁月的变化。无论我在里面尝试了什么,我onChanges()都找不到为什么什么都不起作用......我试过:
- this.formApply.get('currentUKAddress.years').valueChanges.subscribe(data => {
console.log('Form changes', data);
})
- this.formApply.controls.currentUKAddress.get('years').valueChanges.subscribe(data => {
console.log('Form changes', data);
})
- this.formApply.controls.currentUKAddress.controls['years'].valueChanges.subscribe(data => {
console.log('Form changes', data);
})
Run Code Online (Sandbox Code Playgroud)
以及其他东西。在所有情况下,我都会收到 Typescript 编译器错误: 属性不存在于类型 AbstractControl 或 Object 可能为 null。
通过构建基本的Angular CLI项目,我们index.html在运行应用程序时获得以下信息:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SimpleCLI</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<script type="text/javascript" src="inline.bundle.js"></script>
<script type="text/javascript" src="polyfills.bundle.js">
</script><script type="text/javascript" src="styles.bundle.js"></script>
<script type="text/javascript" src="vendor.bundle.js"></script>
<script type="text/javascript" src="main.bundle.js"></script></body>
</html>
Run Code Online (Sandbox Code Playgroud)
下一步是我们的切入点main.ts。当然,名称无关紧要,并且在.angular-cli.json中定义。有人可以澄清以下几点吗?
vendor.bundle.js包含所有Angular代码并负责启动引导过程吗?一旦启动引导过程,应用程序将如何确切地知道要去哪里,即如何触发main.ts?只是财产
"main": "main.ts"
Run Code Online (Sandbox Code Playgroud)
在.angular-cli.json里面定义那?
var elem=document.getElementById('mydiv');
elem.addEventListener('click',function(){...});
Run Code Online (Sandbox Code Playgroud)
执行完上面的代码后elem就是一个HTMLDivElement接口的实例。我的大问题是 addEventListener() 方法到底做了什么。它在哪些 DOM 对象中注册了侦听器以及它是如何注册的(它更改了这些 DOM 对象的哪些属性)。换句话说,我想知道如何知道elem添加了一个监听器,它的哪些属性(所有这些属性都影响到它的原型链)。例如,我知道 Event.prototype 具有诸如type, 之类的关键属性target;但是我无法“连接”它们elem......
我不想找到哪些事件侦听器附加到上述 DOM 节点。我想知道内部程序。
谢谢
我想要一些看起来简单的东西,但我不知道它是否是...通过在 Chrome 的控制台中声明两个 javascript 变量,例如
var x=true;
var y=true;
Run Code Online (Sandbox Code Playgroud)
是否可以通过使用 .x 来查看这些变量 x,y 的内存地址HEAP SNAPSHOT。如果是,怎么办?我想知道 Javascript 中的原始变量是否包含引用或直接指向其数据。稍微解释一下上面的内容:因为x===y我想确定这个比较是否与变量的实际值或其参考地址有关。我知道后者适合对象,我认为前者适合原始值。我对吗?
考虑这个 index.html:
<div id="test-case">
Hello
<button type="button">Press me</button>
</div>
<app> </app>
Run Code Online (Sandbox Code Playgroud)
其中app是root component. 是否可以从应用程序组件内部访问test-case元素并通过 Angular 方法进行操作?我可以使用 vanilla Javascript,但是有 Angular 的方法吗?button click event
我想使用这个自定义钩子来发出 api 请求:
export default function useFetch({ method, url, data = null, config = null }) {
const [response, setResponse] = useState(null);
const [error, setError] = useState("");
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const fetchData = async () => {
try {
api[method](url, JSON.parse(config), JSON.parse(data))
.then((res) => {
setResponse(res.data);
})
.finally(() => {
setIsLoading(false);
});
} catch (err) {
setError(err);
}
};
fetchData();
}, [api, method, url, data, config]);
return { response, error, isLoading };
}
Run Code Online (Sandbox Code Playgroud)
上面的代码并不重要。所以不要太关注它。我的问题是如何在同一个组件中进行两个 api …
成分:
const MyComponent = props => {
const {price} = props;
const result1 = useResult(price);
return (
<div>...</div>
)
}
Run Code Online (Sandbox Code Playgroud)
定制挂钩:
export const useResult = (price) => {
const [result, setResult] = useState([]);
useEffect(() => {
const data = [{price: price}]
setResult(data);
}, [price]);
return result;
};
Run Code Online (Sandbox Code Playgroud)
开玩笑测试:
it('should ...', async () => {
render(
<MyComponent price={300}/>)
)
await waitFor(() => {
expect(...).toBeInTheDocument();
});
});
Run Code Online (Sandbox Code Playgroud)
上面的代码确实发生的是MyComponent,当运行测试时,仅渲染一次而不是两次(当应用程序运行时)。初始渲染后,其中result1是一个空数组,useEffect正在useResult运行,并且由于 导致状态发生变化setResult(data),我应该期望MyComponent …
javascript ×6
angular ×3
dom ×2
html ×2
react-hooks ×2
reactjs ×2
angular-cli ×1
forms ×1
jestjs ×1
jquery ×1
typescript ×1