以下是闭包的案例代码
//测试闭包
document.getElementById('button1').onclick = function() {
var inputNodes = document.getElementsByTagName('input');
var count = inputNodes.length;
for (var index = 0; index < count; index++) {
var inputNode = inputNodes[index];
console.log(inputNode, inputNode.type);
console.log('*******************');
if (inputNode.type == 'text') {
inputNode.onclick = (function(parameter) {
// alert('--' + index);
// alert('==' + parameter);
return fn9(parameter);
})(index);
}
}
};
//测试闭包
function fn9(param){
var f = function fn10() {
alert(param);
}
return f;
}
一些其他的案例代码
function fn1(){
console.log(eval("{}")); //undefined
console.log(eval('{}')); //undefined
console.log(eval("({})")); //Object { }
console.log(eval('({})')); //Object { }
console.log(eval("(" + "{}" + ")")); //Object { }
console.log(eval('(' + '{}' + ')')); //Object { }
// console.log(eval("(" + "{[]}" + ")")); //报错SyntaxError: expected expression, got ']'
// console.log(eval('(' + '{[]}' + ')')); //报错SyntaxError: expected expression, got ']'
console.log(eval({})); //Object { }
console.log(eval({name:"jack"})); //Object { name: "jack" }
console.log(eval('{name:"tom"}')); //tom
console.log(eval("{name:'jerry'}")); //jerry
var test = '{[], [], []}';
console.log(eval(test)); //Array []
// console.log(eval('(' + test + ')')); //报错SyntaxError: expected expression, got ']'
// console.log(eval("(" + test + ")")); //报错SyntaxError: expected expression, got ']'
var test2 = '{}';
console.log(eval(test2)); //undefined
console.log(eval('(' + test2 + ')')); //Object { }
console.log(eval("(" + test2 + ")")); //Object { }
var test6 = {};
console.log(eval(test6)); //Object { }
// console.log(eval('(' + test6 + ')')); //报错SyntaxError: missing ] after element list
// console.log(eval("(" + test6 + ")")); //报错SyntaxError: missing ] after element list
var test3 = '[{}, {}]';
console.log(eval(test3)); //Array [ {}, {} ]
console.log(eval('(' + test3 + ')')); //Array [ {}, {} ]
console.log(eval("(" + test3 + ")")); //Array [ {}, {} ]
var test8 = {};
//true false
console.log(test8 instanceof Object, test8 instanceof Array);
console.log(typeof test8); //object
var test9 = [];
//true true
console.log(test9 instanceof Object, test9 instanceof Array);
console.log(typeof test9); //object
}
fn1();
function fn2(){
return 'hello';
}
var a = fn2();
console.log(a); //hello
function fn3(){
return fn2();
}
var b = fn3();
console.log(b); //hello
function fn4(){
var v = function fn5(){
return '江西省赣州市于都县';
}
return v;
}
var result = fn4();
console.log(result); //function fn5()
console.log(result()); //江西省赣州市于都县
/*
打印出
******我是fn7(),我执行了******
江西于都县666
*/
console.log((function fn6(){
var f = function fn7(){
console.log('******我是fn7(),我执行了******');
return '江西于都县666';
}
return f;
})()());
//测试闭包
document.getElementById('button1').onclick = function() {
var inputNodes = document.getElementsByTagName('input');
var count = inputNodes.length;
for (var index = 0; index < count; index++) {
var inputNode = inputNodes[index];
console.log(inputNode, inputNode.type);
console.log('*******************');
if (inputNode.type == 'text') {
inputNode.onclick = (function(parameter) {
// alert('--' + index);
// alert('==' + parameter);
return fn9(parameter);
})(index);
}
}
};
//测试闭包
function fn9(param){
var f = function fn10() {
alert(param);
}
return f;
}
function fn8() {
console.log('(' + null + ')'); //(null)
console.log(typeof ('(' + null + ')')); //string
console.log('(' + 666 + ')'); //(666)
// console.log('(' + abc + ')'); //ReferenceError: abc is not defined
console.log('(' + 'null' + ')'); //(null)
console.log(typeof ('(' + 'null' + ')')); //string
console.log(eval('(' + null + ')')); //null
console.log(eval('(null)')); //null
// console.log(eval('(' + '' + ')')); //SyntaxError: expected expression, got ')'
console.log(eval('(' + undefined + ')')); //undefined
// console.log(eval('(' + ')')); //SyntaxError: expected expression, got ')'
// console.log(eval('()')); //SyntaxError: expected expression, got ')'
}
fn8();
完!