http://learn.jquery.com/javascript-101/conditional-code/
更新的知识点如下:
1. 知道有这个符号 ===
var foo = 1;
var bar = 0;
var baz = "1";
var bim = 2;
foo == bar; // false
foo != bar; // true
foo == baz; // true; but note that the types are different
alert("===11111"+(foo ===baz)); // false
foo !== baz; // true
foo === parseInt( baz ); // true
foo > bim; // false
bim > baz; // true
foo <= baz; // true
2.switch的另外一种写法
var stuffToDo = {
"bar": function() {
alert( "the value was bar -- yay!" );
},
"baz": function() {
alert( "boo baz :(" );
},
"default": function() {
alert( "everything else is just ok" );
}
};
var foo = "baz";
if ( stuffToDo[ foo ] ) {
stuffToDo[ foo ]();
} else {
stuffToDo[ "default" ]();
}
3. JS编码规范
Google JavaScript Style Guide
4.Array的排序
function descending( a, b ) {
alert("****"+b+"****a****"+a+"((((("+(b-a));
return b - a;
}
var myArray = [ 3, 4, 6, 8 ];
alert(myArray);
alert(myArray.sort( descending ));
这个不知道为什么最后会打印 【8,6,4,3】
5. Array的 forEach函数
function printElement( elem ) {
alert( elem );
}
function printElementAndIndex( elem, index ) {
alert( "Index " + index + ": " + elem );
}
function negateElement( elem, index, array ) {
array[ index ] = -elem;
}
myArray = [ 1, 2, 3, 4, 5 ];
// Prints all elements to the console
myArray.forEach( printElement );
// Prints "Index 0: 1", "Index 1: 2", "Index 2: 3", ...
myArray.forEach( printElementAndIndex );
// myArray is now [ -1, -2, -3, -4, -5 ]
myArray.forEach( negateElement );
alert(myArray);
6.Objects
As it turns out, nearly everything in JavaScript is an object – arrays, functions, numbers, even strings – and they all have properties and methods.
var myObject = { sayHello: function() { console.log( "hello" ); }, myName: "Rebecca" }; myObject.sayHello(); // "hello" console.log( myObject.myName ); // "Rebecca"
7. function
// Function declaration.
function foo() {
// Do something.
}
// Named function expression.
var foo = function() {
// Do something.
};
(2)函数作为参数的2种方式:
// Passing an anonymous function as an argument.
var myFn = function( fn ) {
var result = fn();
console.log( result );
};
// Logs "hello world"
myFn( function() {
return "hello world";
});
// Passing a named function as an argument
var myFn = function( fn ) {
var result = fn();
console.log( result );
};
var myOtherFn = function() {
return "hello world";
};
myFn( myOtherFn ); // "hello world"
8.Testing Type
// Testing the type of various variables. var myFunction = function() { console.log( "hello" ); }; var myObject = { foo: "bar" }; var myArray = [ "a", "b", "c" ]; var myString = "hello"; var myNumber = 3; typeof myFunction; // "function" typeof myObject; // "object" typeof myArray; // "object" -- Careful! typeof myString; // "string" typeof myNumber; // "number" typeof null; // "object" -- Careful! if ( myArray.push && myArray.slice && myArray.join ) { // probably an array (this is called "duck typing") } if ( Object.prototype.toString.call( myArray ) === "[object Array]" ) { // Definitely an array! // This is widely considered as the most robust way // to determine if a specific value is an Array. }