JS learning notes
2014-05-14 04:50
567 查看
1. use js in HTML, use <script> tag, in <head> or <body> or separate file .js.
2. You can break up a code line within a text string with a backslash:
3. variables have dynamic types so can be assigned any data type:
var is used to declare one variable, usually without any initialization but for future use, so it's like a container.
any var outside a function is a global variable
4. The Lifetime of JavaScript Variables
The lifetime of a JavaScript variable starts when it is declared.
Local variables are deleted when the function is completed.
Global variables are deleted when you close the page.
5. Assigning Values to Undeclared JavaScript Variables
If you assign a value to a variable that has not yet been declared, the variable will automatically be declared as a GLOBAL variable.
This statement:
will declare the variable carName as a global variable , even if it is executed inside a function.
6. Hoisting, default behavior of moving declarations to the top of the current scope. That's why variables are be used before declaration.
7. Self-Invoking Funtions
Function expressions can be made "self-invoking".
A self-invoking expression is invoked (started) automatically, without being called.
Function expressions will execute automatically if the expression is followed by ().
But it would generate an error if you place a self-invoking function directly in your code like this:
function() {
code to be executed
}();
You cannot self-invoke a function declaration.
You have to add parentheses around the function to indicate that it is a function expression:
Example:
A function defined as the property of an object, is called a method to the object.
A function designed to create new objects, is called an object constructor.
9. Arguments are Passed by Value <=> Objects are Passed by Reference.
2. You can break up a code line within a text string with a backslash:
var text = "Hello \ World!";But only inside the string.
3. variables have dynamic types so can be assigned any data type:
var x; // Now x is undefined var x = 5; // Now x is a Number var x = "John"; // Now x is a String
var is used to declare one variable, usually without any initialization but for future use, so it's like a container.
any var outside a function is a global variable
4. The Lifetime of JavaScript Variables
The lifetime of a JavaScript variable starts when it is declared.
Local variables are deleted when the function is completed.
Global variables are deleted when you close the page.
5. Assigning Values to Undeclared JavaScript Variables
If you assign a value to a variable that has not yet been declared, the variable will automatically be declared as a GLOBAL variable.
This statement:
carName = "Volvo";
will declare the variable carName as a global variable , even if it is executed inside a function.
6. Hoisting, default behavior of moving declarations to the top of the current scope. That's why variables are be used before declaration.
7. Self-Invoking Funtions
Function expressions can be made "self-invoking".
A self-invoking expression is invoked (started) automatically, without being called.
Function expressions will execute automatically if the expression is followed by ().
But it would generate an error if you place a self-invoking function directly in your code like this:
function() {
code to be executed
}();
You cannot self-invoke a function declaration.
You have to add parentheses around the function to indicate that it is a function expression:
Example:
(function () { var x = "Hello!!"; // I will invoke myself })();8. Functions are Objects
A function defined as the property of an object, is called a method to the object.
A function designed to create new objects, is called an object constructor.
9. Arguments are Passed by Value <=> Objects are Passed by Reference.
相关文章推荐
- Learning Notes for HTML/CSS/JS
- JS learning notes, day 1
- Gnu Make Learning notes 1
- C# Learning Notes
- UliPad Learning Notes
- Erlang Learning Notes
- Linux learning notes
- Concurrency programming Learning notes
- Learning Processing.js - 1
- Android_Learning_Notes_Part 1
- Learning Ext JS Book
- ASP.net Learning notes
- Data structures for game programming learning notes (二)——array
- Data structures for game programming learning notes (六)——stack and queue
- CISSP Learning Notes (1) Information of Exam
- Data structures for game programming learning notes (三)——bitvector
- Some C Learning Notes
- Data structures for game programing learning notes (一)—— template
- notes/tips during sql tutorial learning
- Linux learning notes