JavaScript this keyword

JavaScript this keyword

In object-oriented languages, this represents a reference to the current object.

But in JavaScript, this is not fixed. It will change with the change of execution environment.

  • In a method, this represents the object to which the method belongs.
  • If used alone, this represents a global object.
  • In the function, this represents the global object.
  • In the function, in strict mode, this is undefined.
  • In an event, this represents the element that receives the event.
  • Methods like call() and apply() can reference this to any object.

this keyword

this: the object of the current class

this is the attribute information in the object that can be obtained inside the method

this can also distinguish between local variables and member variables

Construction method

Method automatically called when creating an object


public class name (pass parameter){


be careful:

​ 1. There is no return value

​ 2. When we create new, the constructor method is called automatically

Function: when creating an object, give the object or set attribute information

java will automatically send every structure without parameters by default, but if you write a constructor, the system will no longer be a real gift,

package elephant;

public class Hreo {
    String name;
    String skill_1;
    String skill_2;
    String skill_3;
    String skill_4;
    String skill_5;
    String skill_6;

    public Hreo(String name){;


    public Hreo(String name,String skill_1,String skill_2,String skill_3,String skill_4,String skill_5,String skill_6){;


    public void fight(){
        System.out.println("go to the front to fight the enemy");
        System.out.println("A skill of Li Bai"+this.skill_1);
        System.out.println("Li Bai's second skill"+this.skill_2);
        System.out.println("Li Bai's three skills"+this.skill_3);
        System.out.println("Li Bai's ordinary🐔skill"+this.skill_4);
        System.out.println("Li Bai's skill of returning to the city"+this.skill_5);
        System.out.println("Li Bai's disciplinary skills"+this.skill_6);


    public static void main(String[] args) {
        Hreo h = new Hreo("Li Bai, Ming Jian·Drag shadow","Plunge","Decontrol","Big move","🍎A","delivery","Cause harm");


(computer advanced language keywords)

Like in VB, C #, C, C++ , Java, PHP, Objective-C, JavaScript, we can see static as keyword and function It also appears in other high-level computer languages, such as FORTRAN, ALGOL, COBOL, BASIC, LISP, SNOBOL, PL/1, Pascal, PROLOG, Ada and other languages, but it has different functions. For its specific functions, readers can refer to it when they need it.

package exercise;

public class Person {
    String name;
    static String country = "Qing Dynasty";//Modified by static, it is shared
    String address;//address

    public Person(String name,String address){;
    public static void main(String[] args) {
        Person p1 = new Person("Tie Zhu Zhao","Eight Hutongs");
        Person p2 = new Person("Li Xiaohua","Chaoyang Gate");

        //My Qing Dynasty is dead = "Republic of China";



​ 1. data sharing

​ 2. Belonging to a class Does not belong to the object

​ 3. Generated prior to the object

Java modifier

The Java language provides many modifiers, which are mainly divided into the following two categories:

  • Access modifier
  • Non access modifier

Modifiers are used to define classes, methods, or variables, and are usually placed at the front of a statement. Let's illustrate with the following example:

public class ClassName {
   // ...
private boolean myFlag;
static final double weeks = 9.5;
protected static final int BOXWIDTH = 42;
public static void main(String[] arguments) {
   // Method body

Access modifier

In Java, access control characters can be used to protect access to classes, variables, methods and construction methods. Java supports four different access rights.

  • default: visible in the same package without any modifiers. Use objects: classes, interfaces, variables, methods.
  • private: visible in the same category. Use object: variable, method. Note: cannot decorate class (external class)
  • public: visible to all classes. Objects used: classes, interfaces, variables, methods
  • protected: visible to classes and all subclasses in the same package. Use object: variable, method. Note: you cannot decorate classes (external classes).

We can explain the access rights through the following table:

Modifier Current classIn the same bagDescendants (same package)Descendants (different packages)Other packages

Default access modifier - do not use any keywords

Variables and methods declared with default access modifiers are visible to classes within the same package. The variables in the interface are implicitly declared as public static final, while the access permission of the methods in the interface is public by default.

Keywords: Java Programming Interview Class Polymorphism

Added by Morbid on Thu, 17 Feb 2022 20:00:19 +0200