search
HomeWeb Front-endJS Tutorialjs basic entry function

js basic entry function

Mar 31, 2018 am 11:42 AM
javascriptgetting Startedfunction

This article introduces you to the basic introductory functions of js. It is more detailed and basic. Friends in need can take a look


1: Function method :


Function: Encapsulate a piece of code! This code implements a certain function. When this function is needed, the function is called.


java:


Modifier return type method name (Data type variable name, multiple) {


Method body;


                                                     

1.1 The first way to define a function:


js

function function name (parameter list) {

Function body;

Function details:

1. The function must be executed after being called.

2. If the function has a return value, you can directly use the data returned by return in the function body. Functions in JS do not have the restriction of return value type.

3. If the function needs to receive parameters, write the variable name directly without the var keyword.

4. There is no concept of function overloading in JS. If there are multiple functions with the same name in JS, the previous functions will be overwritten.

5. In JS, if a function needs to accept parameters but does not pass them, then the variables are all undefined

6. Regardless of whether the function in JS accepts parameters or not, when we call it, Parameters can be passed to it.

7. There is a built-in array (arguments) parameter in the function in JS to receive all the data passed. Using arguments, this parameter is actually an array object itself.

Note: If you use a function in JS later and need to accept parameters, define the variables on the function. If not, do not define them. Arguments array operations are rarely used.

<script type="text/javascript">
		//定一个函数----求和
		//1.在js中,参数列表中,不能书写var 。 如果一个函数需要参数,直接去定义一个参数的名字就可以了
		//2.函数,一定要被调用才可以去执行。 调用的方式: 函数名(); 
		//3.如果函数需要返回值,直接在函数中去书写return;
		//4.在js中没有重载的概念的。如果函数名相同,后面的会把前面的进行覆盖。
		/* function sum(a , b ){
			alert(a+b);
		}
		 	sum(2,3);//方法不调用,不执行
		 	 */
// 	======================================================	 	 
		 /* function sum(a , b ){
		 		return a+b;
		 	}
		 	var s = sum(2,5) ; // 如果需要返回值,就直接return 	
		 	alert(s); */
// =============================================================		 	
		 	function sum(a , b ){
		 		alert(1);
		 		return a+b ;
		 	}
		 	
			function sum(a , b ,c ){  //js中没有重载的含义,如果方法名相同,那么后面的会把前面的覆盖
		 		alert(2);
		 		return a+b ;
		 	}
		 	
		 	var s = sum(2,4);//按java来说两个参数应该会去调两个参数的
		 	alert(s);
	</script>

Bind a method:

1.2 The second definition method of function:

Writing method: var function Name = new Function("Parameter list", "Function body");

The first parameter represents the parameter list

The second parameter represents the function body.

#The function list and parameter body are both defined as strings.


Writing functions are too complicated, so developers rarely use them

1.3 The third way to define a function: anonymous function:


Writing method:


function(参数列表){


         函数体;


       }

       



事件:可以理解为一些行为或者动作。如果该行为或者动作有意义,需要我们去做一些事情,此时可以通过事件去调用js中的函数,实现某些功能。


<html>
  <head>
    <title>18函数的第三种定义方式.html</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<script type="text/javascript">
		/*  
			1、页面的加载顺序
			2、事件: 一个动作或者是一个操作。
			3、window.onload === 表示 页面加载完毕 会触发
			4、如何为事件去绑定一个函数   对象.事件名=function(){}
		*/
	
	/* // 此时相当于给函数一个名字,demo
	   var demo = function(){
			alert("demo");
		}
		//正常的方式函数名()去调用
	   demo(); */
	  // window.onload 表示一个事件。 表示当前的页面全部加载完成。
	  /*  
	  		考虑js的执行顺序。或者html的页面的加载顺序。
	  		浏览器加载页面的时候,从上向下,逐步的去加载
	  */
	  // 事件: 理解为一件事,或这是一个动作。 例如按钮,点击的时候。
	  
	  alert("window.onload 之前");
	  	// 函数的第三中定义方式,通常与一个事件进行绑定使用。
	  window.onload =  function(){
		alert("demo");
     	  		// 1、把按钮的标签获取到 --document.getElementById根据标签的id的属性,获取当前标签
		  var _button = document.getElementById("b1");
		  // 对象.事件名 = function(){}
		  _button.onclick = function(){
		  		alert("您点击了按钮");
		  }

	  }
	</script>
  </head>
  
  <body>
  	<h1 id="这是h">这是h1</h1>
  	<input type="button" id="b1" value="按钮">
  </body>
</html>

1.4全局函数:



js为我们提供了几个全局函数。我们可以直接使用的函数。



url:统一资源定位符

http://www.baidu.com/s/fasdf/basdf

http:// 协议:

www  万维网。 主机名

.baidu.com 域名

/s/fasdf/basdf ---- 资源路径。(资源:互联网中存在的一些文件或者是程序。 例如:html页面。 图片,视频,音频。。。。)

uri:统一资源标识符

/s/fasdf/basdf   uri实际上就是一个资源路径。

url 是一种具体的uri 。所以uri的表示范围大于url。(相对来说)

uri 的编码。浏览的地址栏中的内容,会提交到一个服务器。 涉及到http协议 。 不支持中文在url的存在。 需要进行uri的编码。



相关推荐:

掌握js函数

进一步了解js函数

js函数和变量的提升及闭包讲解

The above is the detailed content of js basic entry function. 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
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.