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 ObjectsA 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.
本文深入探讨了JavaScript中使用<script>标签的位置、文本字符串断行、变量声明与类型、作用域、变量赋值与生命周期等核心概念。通过具体实例阐述了如何在不同场景下灵活运用JavaScript,并解释了变量赋值于未声明变量时的自动全局作用域特性及变量作用域的生命周期。
2604

被折叠的 条评论
为什么被折叠?



