search
HomeBackend DevelopmentPHP TutorialLearn more about js functions

Learn more about js functions

Mar 19, 2018 pm 03:47 PM
javascriptlearnfurther


This article mainly shares with you a further understanding of js functions, hoping to help everyone use js functions better.
1. Custom function

1. Through the function keyword

function function name ([parameter]){

Code segment;

return return value;

}

Note: The function name should not contain special characters;

It is best to have a clear meaning of the function name;

Function name is best to follow the hump markings or lower lines;

function names are strictly distinguished by the case; It can have parameters or no parameters;

The function returns a value through return. If there is no return, it returns undefined by default;

The function will not be executed if it is not called;

2. Anonymous function

Function expressions can be stored in variables, and variables can also be used as a function;

Anonymous functions can be Passed as a parameter to other functions, the receiver function can complete certain functions through the passed function;

Can perform certain one-time tasks through anonymous functions;

3. Construct the function through Function()

2. Call the function

1. Call it as a function

Through the function name ( ) to call, if there are parameters, just pass the corresponding parameters; The default global object in HTML is the html page itself, so the function belongs to the html page, and the page object in the browser is the browsing window (window ). Therefore, the function will automatically become a function of the window object, and can also be called through window.function();

2. Global object

When the function is not called by its own When the object is called, the value of this will become the global object. In the web browser, the global object is the browser window to the window object; When the function is called as a global object, the value of this will become the global object. Object, using the window object as a variable can easily cause the program to crash;

3. Call the function as a method

You can define the function as a method of the object to call;

4. Use the constructor to call the function

If the new keyword is used before the function call, the constructor is called; 5. As a response Drop function calls

call();

apply();

3. Parameters

Functions can have parameters or no parameters, if defined Parameters, no value is passed when the function is called, and the default setting is undefined;

If the parameters passed exceed the parameters defined when calling the function, js will automatically ignore the excess parameters;

Default values ​​cannot be written directly in js. Default value effects can be achieved through arguments objects;

Functions with variable parameters can be implemented through arguments objects;

Pass parameters by value and modify variables in the function body Making modifications will not affect the variable itself;

Passing parameters through objects and changing the variable pair in the function body will affect the variable itself;

4. Variable scope

1. Local Variables

Variables declared within the function body are only used within the function body;

2. Global variables

Variables declared outside the function are used from the beginning of the variable declaration to the end of the script. Can be used;

3. Note

Try to control the number of global variables, which may easily cause bugs;

It is best to always use the var statement to declare variables;

5. Global functions in js

1.parseInt(string,radix)

Return the value converted into an integer;

2.parseFloat(sring)

Return the value converted into floating point type

3.isFinite(value)

Detect whether a value is infinite, if number is nan or infinity or -infinity number, then return false;

4.isNaN(value)

Detect whether a value is NaN, if the value is nan, return true, otherwise return false;

5.encodeURL(url)//encodeURIComponent()

Encode the string into url, ASCII punctuation, this function will not escape,,/? : @&+=¥#, you can use the encodeURIComponent() method to encode ASCII punctuation marks with special meanings respectively;

6.decodeURI//decodeURIComponent()

Decode a certain encoding URI;

7.escape()

Encode the string;

The escape() function can encode the string so that it can be read on all computers Get the string;

will not encode numbers and letters, nor will it encode the following punctuation marks. *@-_+./ All other characters will be replaced by escape sequences;

The escape() function cannot be used to encode URI

8.unescape()

Decode the function with escape encoding

9.eval()

Execute js string as a script

If the parameter is an expression, the eval() function will execute the expression. If the parameter is a js statement, the js statement will be executed;

The eval() function is a dynamic code executed by a function, which is much slower than directly executing the script;

Use the eval() function with caution and try not to use it to ensure the security of the program;

10.Number(obj)

Convert the value of the object into a number;

If the value of the object cannot be converted into a number, NaN is returned;

If the object is date object, returns the number of milliseconds elapsed from January 1, 1970 to the limit;

11.string

Convert the value of the object into a string; and Same as toString;

Related recommendations:

Explanation of promotion and closure of js functions and variables

In-depth analysis of JS functions

Summary and sharing of knowledge points related to js functions

The above is the detailed content of Learn more about js functions. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft