Home > Web Front-end > JS Tutorial > Introduction to the four calling modes of JavaScript and this example_javascript skills

Introduction to the four calling modes of JavaScript and this example_javascript skills

WBOY
Release: 2016-05-16 17:05:48
Original
936 people have browsed it

In addition to the formal parameters defined when calling JavaScript, each function accepts two additional parameters: this and arguments. This is very important in object-oriented programming and depends on the calling mode.

JavaScript has four calling modes, method calling mode, function calling mode, constructor calling mode and apply calling mode. These modes differ in the initialization key parameter this.

Method calling pattern: When a function is saved as a property of an object, we call it a method. When a method is called, this is bound to the object. If the call expression contains an attribute expression (i.e. a .dot expression or a [script] subscript expression), then it is treated as a method call.

Copy code The code is as follows:

var myObject = {
value: 0;
increment: function(inc){
this.value = typeof inc === 'number' ? inc : 1;
}
};

myObject.increment();
document.writeln(myObject.value);//1

myObject.increment(2);
document.writeln(myObject.value);//2

Methods can use this to access the object, so it can get values ​​from the object or modify the object. The binding of this occurs when calling. This super late binding allows the function to be highly reusable. The context methods to which they belong can be obtained through this, which are called public methods.

Function calling mode: When a function is not an attribute of an object, then it is called as a function var sum = add(3, 4);//sum value is 7
When the function starts with When this pattern is called, this is bound to the global object.

Constructor calling mode: JavaScript is a language based on prototypal inheritance. This means that properties can be inherited directly from other objects. The language is classless.
If you call a function with new in front of it, a new object will be created that hides the prototype member connected to the function, and this will be bound to that new object.

Apply calling mode: Because JavaScript is a functional object-oriented programming language, functions can have methods.
The apply method allows us to construct a parameter array and use it to call the function. It also allows us to select the value of this.
The apply method accepts two parameters, the first one will be bound to the value of this, and the second one is a parameter array.
Related labels:
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