Basic knowledge of JS function

Source: Internet
Author: User

return value.
function box () {
Alert ("Remember to invoke Oh!") ")
}
Box (); The function itself is not functioning and needs to be called before it can be executed.

function box (name,job) {
Alert ("Name:" + name + "work:" + job);

Box (); When calling a function, if you do not enter the parameters inside, this will output the name: undefined work undefied
Box ("123", "Web Front End")//Name: 123 working Web front end. If only one parameter is passed in, the first one is shown, and the second one is still undefined.

function box () {
Return "12345, hit the tiger up the hill"//return said to put this sentence string back.
}
Box (); The function has been called, but the result is not displayed.
Console.log (Box ());

function box () {
return 1;
return 2;
}
Alert (Box ()); Only pops up 11 times. When a function encounters the first return, it terminates the function and continues execution.

function box () {
var sum = 0;
for (var i = 0;i < arguments.length;i++) {
Sum + = Arguments[i];
}
return sum;
}
Alert (Box (1,4,5));//dynamic parameter, the function can be used to calculate the parameters and. The result of the calculation is 10.

function box (num) {
if (num <= 1) {
return 1;
}else{
Return Num*box (num-1);
}
}
Alert (Box (4)); This is a factorial! Calculate 4*3*2*1;

function box (num) {
if (num <= 1) {
return 1;
}else{
Return Num*box (num-1); /
}
}
Alert (Box (4)); If the function name changes, you can use Arguments.callee to invoke the function itself to implement recursion.

This refers to the object where the function is to perform the action, or the scope where the function call statement is located. PS: When a function is called in the global scope, the object referenced by this is window.
Window is the largest object of the JS example, and also the outermost object.
alert (window); Print the result as Object window;
Alert (typeof window);//print result is object;
alert (this); This currently represents window.

Window.width = "100px";
function Saywidth () {
Alert (this)-this refers to the Window object
}
Saywidth (); Popup Object Window;


Window.width = "100px";
function Saywidth () {
alert (this.width); The execution is dynamic, and the first execution is in the window environment, and the second time is executed under box.
}
Saywidth ();

var box = {
Width: "200px"
};
Box.saywidth = Saywidth;
Box.saywidth ();

This code first pops up 100px, and pops up 200px.

--function properties and methods:
The-->ecmascript function is an object, each function contains two attributes, Length (which indicates the number of named arguments the function expects to receive) and prototype


Variables and scopes:
A variable may contain two values of different data types: the base type and the reference class type value. Primitive type values refer to simple data segments, whereas reference types refer to objects that may consist of multiple values.


Passing parameters

The function box (num) {//is passed by value, and the passed parameter is the base type.
num + = 10;
return num;
}
var num = 50;
Alert (Box (NUM)); 60;
alert (num); 50;

Basic knowledge of JS function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.