From Evernote: |
JavascriptClipped from: http://jqfundamentals.com/chapter/javascript-basics |
只有五种基本类型(primitives),其他都是对象
As it turns out, most everything we work with in JavaScript is an object — in fact, there are only five kinds of values that are not objects:
- strings (text) : 'Aliz'
- booleans (true/false)
- numbers
undefined
null
对象声明和使用:
var person = {
firstName : 'Boaz',
lastName : 'Sender'
};
log( 'First name: ' + person.firstName ); // dot notation
log( 'Last name: ' + person[ 'lastName' ] ); // bracket notation,需加引号
函数命名有两种方法,只用第一种
var addTwoNumbers = function(a, b) {
return a + b;
};
function addTwoNumbers(a, b) {
return a + b;
}
Bottom line: naming functions using the function declaration approach can have unexpected results if you don't fully understand a feature of JavaScript known as hoisting. The details of hoisting are beyond the scope of this guide, but for now we'll stick to assigning function expressions to variables.
变量:
- you should almost always declare variables with a
var
statement - variables declared inside of a function using a
var
statement are not available outside of that function - variables declared without a
var
statement are always global
Variables that are declared inside a function with a var
statement are only available inside of the function; this is generally desirable, and all variables should be declared with a var
statement unless they are intended to be global — that is, available anywhere in your code. This usually isn't what you want, unless you want it to be possible for other code to change the value of the variable.
对象内的函数:
var person = {
firstName : 'Boaz',
lastName : 'Sender',
greet : function() {
log( 'Hi, ' + this.firstName );
}
};
person.greet(); // logs 'Hi, Boaz'
var sayIt = person.greet; // store the method in a variable
sayIt(); // logs 'Hi, undefined' -- uh-oh 因为 this 含义改变了
this
. It refers to the object that is the context in which the function was called.
When we store the .greet()
method in a variable sayIt
and then call sayIt()
, the context object changes to the global window
object, not the person
object. Since the window
object doesn't have a property firstName
, we getundefined
when we try to access it.
What's a developer to do? First, be aware that storing object methods in variables can have unintended consequences for the meaning of this
. Second, be aware that you can force the meaning of this
to be what you want it to be, using the.call()
or .apply()
method on the function itself.
sayIt.call( person ); // logs 'Hi, Boaz'
Array 使用方法
var myArray = [ 'a', 'b', 'c' ];
var firstItem = myArray[ 0 ];
var secondItem = myArray[ 1 ]; // access the item at index 1
log( secondItem ); // logs 'b'
var len = myArray.length;
var i;
for (i = 0; i < len; i = i + 1) {
log( 'item at index ' + i + ' is ' + myArray[ i ] );
}
Logic operation:
五种 false
As it turns out, most values in JavaScript are truthy — in fact, there are only five values in JavaScript that are falsy:
undefined
(the default value of declared variables that are not assigned a value)null
NaN
("not a number"):将非数学类型进行数学操作,会得到 NaN,如 'five' - '4'0
(the number zero)''
(an empty string)
'0'
), and all numbers other than 0
.&&
) and 逻辑或 (||
) operations.
0 comments:
Post a Comment