Home > Web Front-end > JS Tutorial > Analysis of usage examples of this in js_javascript skills

Analysis of usage examples of this in js_javascript skills

WBOY
Release: 2016-05-16 16:21:01
Original
1140 people have browsed it

This article analyzes the usage of this in js with examples. Share it with everyone for your reference. The specific analysis is as follows:

Example 1: The first way to call this - call it in function form. It represents the global object window

Copy code The code is as follows:

Example 2: The second way to call this - in the form of an object property. It represents the object. In the example, onclick is an attribute of the p element object
Copy code The code is as follows:





abcd




Example 3: The third way to call this-call it as a constructor.
Copy code The code is as follows:

Description:
(1) If you call the dog() function directly instead of calling it by creating an object, it is equivalent to the first call, that is, this represents the global object window.
(2) There is no concept of class in js, but there is the concept of object;
(3) Objects can be created using var = objName{name:'abc',age:40}, or through constructors;
(4) What is a constructor: When an ordinary function is called with "new function name ()", this function acts as a constructor.

Example 4: The fourth way to call this - call and apply methods

Copy code The code is as follows:





hello world



I hope this article will be helpful to everyone’s JavaScript programming design.

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