Home > Web Front-end > JS Tutorial > Definition, calling and issues that should be paid attention to in JavaScript functions

Definition, calling and issues that should be paid attention to in JavaScript functions

零下一度
Release: 2017-04-17 14:46:33
Original
1399 people have browsed it

This article mainly introduces the definition of JavaScript function-calling and precautions. Friends who need it can refer to it

Function definition

Function statement definition


function(a,b){
 return a+b; 
}
Copy after login

Expression definition


var add = function(a,b){return a+b};
Copy after login

//Function expressions can contain names, which is useful when recursing var f = function fact(x){ if(x<=1) {return 1; }else { return x*faxt(x-1); } }

Function defined as an expression, the name of the function is optional. If the function definition expression contains a name, the local scope of the function will contain a name bound to the function object. In fact, the name of the function will become a local variable inside the function

Function Naming Specifications

1.The first character of like_this() is lowercase. When it contains multiple words, the words are separated by underscores

2.The first character of likeThis() is lowercase , when it contains multiple words, the first letters of the words except the first word use university letters

3. Internal functions or private functions are usually prefixed with an underscore

Note: Function The declaration statement is advanced to the top of the external script or external function, so it can be called by code that appears before it is defined.

Before defining a function in expression mode, you must assign it to a variable. Therefore, functions defined in expression mode cannot be called before they are defined.

Function return value

  • The return statement returns the value of the function to the caller .

  • If there is no return statement, it returns undefined

Nested function

  • Nested functions can access the parameters and variables of their functions

  • But nested functions cannot appear in loops, conditionals, or try/cache/finally/with statements

Function call

  • As function

  • As method

  • As constructors

  • Indirect calls through their call() and apply() methods

As a function call

add(1,2)

According to the provisions of ECMAScript3 and non-strict ECMAScript5 for function calls, the calling context (this value) is a global object. In strict mode, however, the calling context is undefined.

//Define a function and call a function to determine whether the current script is running in strict mode
var strict = (function(){return this;}());

As a method call


var o = {
 m:1,
 n:2,
 add:function(){
  this.result = this.m+this.n;
  }
}
Copy after login

There is a major difference between method calls and function calls. ----Calling context

o.add() The calling context of add is o, that is: this
o.add() is equivalent to o['add']()
method chain : When the method does not need to return a value, it is best to directly return this
this as a keyword, not a variable or an attribute name. JavaScript syntax does not allow assigning a value to this
Unlike variables, the keyword this has no restrictions on its effect. Nested functions will not inherit this from the function that calls it. If you want to access this of an external function, var self= this ;Save in a variable
If the nested function is called as a function, its this value points to the object that called it. If called as a function, this is either a global object (non-strict mode) or undefined (strict mode)

Constructor call


var o = new Object();
Copy after login

Constructor calls, ordinary function calls and method calls are processed in actual parameters. There are differences in aspects such as calling context and return value.

The parentheses can be omitted for any constructor call without formal parameters.


var o = new Object();
var o = new Object;
//等价
Copy after login

The constructor call creates a new empty object. This object inherits the prototype attribute of the constructor. This function attempts to initialize the newly created object and destroys the object. As a context, the constructor can use the this keyword to refer to the newly created object.

The context of the new o.m() call is not o.
The constructor does not use return. If the constructor explicitly returns an object using the return statement, then the value of the calling expression is this object. If the constructor does not return a value, or returns a primitive value, the return value is ignored and a new object is used as the return result.

Indirect call

call()
appy()

The above is the detailed content of Definition, calling and issues that should be paid attention to in JavaScript functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template