JavaScript study notes (nine) - Object

Author: This article is a blogger LI Qin original article, shall not be reproduced without the bloggers allowed.
This link: https://blog.csdn.net/weixin_42320142/article/details/102672070

What is the function?

Or functions are reusable blocks of code when it is called for execution of the event driven.


JavaScript function syntax

Function block is wrapped in braces using Image function:

function functionname(){
    // 执行代码
}

When the function is called, it will execute the code within the function.
You can directly call a function when an event occurs (such as when the user clicks a button), and JavaScript can be invoked at any position.
JavaScript is case sensitive. Keywords function must be lowercase, and must be the same case with the function name to call the function.


Function calls with arguments

When calling a function, that can be passed values, these values are referred to as parameter.
These parameters may be used in the function.
You can send any number of parameters, (,), separated by commas:

myFunction(argument1,argument2)

When you declare a function, declare the parameters as variables:

function myFunction(var1,var2){
	代码
}

Variables and parameters must appear in the same order. The first variable is the first parameter is passed a given value, and so on.

<p>点击这个按钮,来调用带参数的函数。</p>
<button onclick="myFunction('Harry Potter','Wizard')">点击这里</button>
<script>
function myFunction(name,job){
    alert("Welcome " + name + ", the " + job);
}
</script>

Function returns with a value

grammar:

function myFunction(){
    var x=5;
    return x;
}

Local JavaScript Variables


In JavaScript variables declared inside a function (using var) is a local variable, so it can only be accessed within the function. (The variable scope is local).
You can use the same name as a local variable in different functions, because only a function of the variable is declared in order to identify the variables.
As long as the function is completed, the local variable will be deleted.


Global JavaScript variable

Variables declared outside a function are global variables, all scripts and functions on the page can access it.


Survival of the JavaScript variable

Lifetime JavaScript variables from the time they are declared.
Local variables will be deleted after the function is run.
Global variables will be deleted after the page is closed.


JavaScript variables to assign values ​​to undeclared

If not yet declared value to the variable, the variable will be automatically as an attribute of the window.

carname="Volvo";

The declaration window of a property carname.
Non-strict mode undeclared variable assignment to create a global variable, the global object is configurable properties can be deleted.

var var1 = 1; // 不可配置全局属性
var2 = 2; // 没有使用 var 声明,可配置全局属性

console.log(this.var1); // 1
console.log(window.var1); // 1

delete var1; // false 无法删除
console.log(var1); //1

delete var2; 
console.log(delete var2); // true
console.log(var2); // 已经删除 报错变量未定义

Guess you like

Origin blog.csdn.net/weixin_42320142/article/details/102672070