每当我初始化一个名为'name'的变量时,它都会通过页面保持其值.
像这样:
page1.html
<html>
<script>
var name = prompt("What's your name?");
alert(name);
</script>
<a href='page2.html'> Page2</a>
</html>
Run Code Online (Sandbox Code Playgroud)
page2.html
<html>
<script>
alert(name);
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
因此,在两个页面中,变量名称保留了在第一页的提示中给出的值,两个页面提醒相同的事情,有人可以解释为什么会发生这种情况吗?
可能重复:
javascript中自执行函数的目的是什么?
请问,有人可以向我解释一下JS中的含义是什么:
var obj = (function(){
// code
})()
Run Code Online (Sandbox Code Playgroud)
谢谢
我想将一个全局参数传递给a中的函数setTimeout,但我希望该值在setTimeout解释时保持不变:
var a=0;
setTimeout(function(){
console.log(a);
},5000,a)
a=1;
//output is 1 and should be 0Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?我已经搜索了Stack Overflow的答案,但没有找到任何答案.
我现在已经约6个小时了.我通过几个Lint工具和各种其他在线测试处理它,我无法正确关闭以下声明.在最后一行产生了一个持续的错误,Lint说a}是必需的,我尝试过},}}}} w /和w/out的许多其他变体.以及2x 3x或更多右括号,括号和分号的许多其他变体.我检查并重新检查所有开始和结束语句,以确保它们匹配.我想我只是没有看到什么!
如果一双新眼睛可以看到这一点,看看我错过了什么,做错了什么.
(function ($) {
"use strict";
var slider = null;
$(document).on("mouseover", '.bb_menu_item', function () {
slider.stopAuto();
slider.goToSlide($(this).attr('index'));
});
$(document).on("mouseout", '.bb_menu_item', function () {
slider.startAuto();
});
$(document).ready(function () {
if ($('#bbSlider').length) {
slider = $('#bb_slider').bxSlider({
controls: false,
pager: false,
auto: true,
slideWidth: 1030,
responsive: true,
mode:"fade"
});
}
$('.tab-linker').click(function () {
window.location.href = 'http://t.com/?page_id=1302#tab-id-2';
//return false;
location.reload(false);
});
// Custom homepage slider
$(".slider-home").click(function (event) {
event.preventDefault();
window.location = "http://t.com/";
});
//
$(".slider-services").click(function (event) {
event.preventDefault(); …Run Code Online (Sandbox Code Playgroud) 我理解表达式在执行上下文到达之前不会"存在".我只是想知道是否有任何区域使用函数表达式比普通函数语句更好,您不必担心函数何时会被保存到内存中.
我完全了解它们如何在差异中工作,我只是对表达的用途感到好奇.
你好我需要一个函数来点击一个标签时运行.我正在使用onclick =""但似乎该函数在页面加载时运行,而不是等待单击a标记时运行.我该怎么做才能使函数仅在单击a标签时运行?
这是我的代码.
HTML:
<a class="req_btn" onclick="conversionOne()">text</a>
<script type='text/javascript'>
(function conversionOne() {
alert("something");
})();
</script>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我发现我有两个函数共享一些代码所以我决定把它放在模板函数中:
function template(callback){
var all, these, variables, are, used, inthe, callbackFunction;
for (var i=0; i<10; i++){
callback();
}
}
function myFirstFunction(){
//do something with all those variables
}
function mySecondFunction(){
//do something else
}
Run Code Online (Sandbox Code Playgroud)
因此,对于每一个我称之为功能template(myFirstFunction)和template(mySecondFunction)
有没有什么方法可以使用我的函数中的模板函数中定义的所有变量而不通过参数传递它们?
我的函数实际上是一个对象的方法:
function MyObject(){
};
MyObject.prototype.template = function(){
var all, these, variables, are, used, inthe, callbackFunction;
for (var i=0; i<10; i++){
callback();
}};
MyObject.prototype.myFirstMethod = function(){
this.template(function(){
//doSomething with all those variables
});
};
MyObject.prototype.mySecondMethod = function(){ …Run Code Online (Sandbox Code Playgroud)