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

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:

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: