您的位置:首页 > Web前端 > JavaScript

JavaScript学习笔记1--语法篇摘录

2015-04-17 17:47 309 查看
Here are the falsy values:

false
null
undefined
the empty string ''
The number 0
The number NaN

/////////////////////////////////////////////////////////////////////////////////

The break statement can optionally have a label that will cause an exit from the labeled statement, like goto....

var result = false;
point:
{
var obj = { key: 1 };
for (var key in obj) {
// ...
break point;
}
result = true;
}
alert(result);


/////////////////////////////////////////////////////////////////////////////////

The equality operator: ===

/////////////////////////////////////////////////////////////////////////////////

Most languages with C syntax have block scope. All variables defined in a block (a list of statements wrapped with curly braces) are not visible from outside of the block. But JavaScript does not have block scope even though its block syntax suggests that
it does.

var foo = function ( ) {
var a = 3, b = 5;
var bar = function ( ) {
var b = 7, c = 11;
// At this point, a is 3, b is 7, and c is 11
a += b + c;
// At this point, a is 21, b is 7, and c is 11
};
// At this point, a is 3, b is 5, and c is not defined
bar( );
// At this point, a is 21, b is 5
};


/////////////////////////////////////////////////////////////////////////////////

对于nodes的onclick提示的i都指向同一个内存地址,所以运行的效果提示的都是nodes的个数。
// BAD EXAMPLE
// Make a function that assigns event handler functions to an array of nodes the wrong way.
// When you click on a node, an alert box is supposed to display the ordinal of the node.
// But it always displays the number of nodes instead.
var add_the_handlers = function (nodes) {


var i;

for (i = 0; i < nodes.length; i += 1) {


nodes[i].onclick = function (e) {


alert(i);


};


}


};
// END BAD EXAMPLE


// BETTER EXAMPLE
// Make a function that assigns event handler functions to an array of nodes the right way.
// When you click on a node, an alert box will display the ordinal of the node.
var add_the_handlers = function (nodes) {


var i;

for (i = 0; i < nodes.length; i += 1) {


nodes[i].onclick = function (i) {


return function (e) {


alert(e);


};


}(i);


}


};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: