Static Method, Example Method and Prototype Method

There are three methods in javaScript: static method, instance method and prototype method.

1. Static methods are directly defined to constructors. Static methods are called using class names. Method names, but instances created by constructors cannot call static methods.

2. Instance method is to point this to the newly created instance on the constructor and add method to each newly created instance in the way of this. method name = function () {...}. This method is called instance method. Each instance defines this method when it is created, and each instance's method is unique and not shared with other instances. Instance method is invoked by instance. method name. Constructor cannot call instance methods.

3. The prototype method is defined on the prototype object of the constructor, which is common to all instances, but the constructor that creates the instance can not call the prototype method. The way an instance calls a method on the constructor prototype object: the constructor name. prototype. method name.

 

(But in most places, the prototype method here is called the instance method, and the example method here does not specify which method is correct or not. For the time being, this classification will be done first, and then the exact answer will be found before the modification is made.

 

Examples of static methods are as follows:

function Person (){
    
}

Person.sayHello = function(){   // This form defines static methods.
    console.log("Hello!");
}

var p1 = new Person();
console.log(p1);    // Object{ }
Person.sayHello();  // Hello!
p1.sayHello();      // Error reporting. The sayHello method in Person is a static method. The created instance can not be invoked, and it will report an error once invoked. The constructor itself can be called.

 

Examples of example methods:

function Person (){
    this.sayHello = function(){   // sayHello, defined in this form, is the instance method.
        console.log("Hello!");
    }
}

var p1 = new Person();
console.log(p1);    // Object { sayHello: sayHello() }
Person.sayHello();  //Error reporting. The constructor does not define this method. The method defined by this is only for instance, only instance can call it.
p1.sayHello();      //Instances can be invoked and output "Hello!"

var p2 = new Person();
console.log(p2);
p2.sayHello();
console.log(p1.sayHello == p2.sayHello);   // false
console.log(p1.sayHello === p2.sayHello);  // Output false, each instance will define the sayHello method when it is created. Each instance's method is unique and not shared with other instances, so each instance's method is not the same.

 

Examples of prototype methods are as follows:

function Person (){
   	
}

Person.prototype.sayHello = function(){    // The prototype method is defined.
    console.log("Hello!");
}

var p1 = new Person();
console.log(p1);
p1.sayHello();   //The instance can call the prototype method and output: "Hello!"
Person.sayHello();  //Error reporting. A constructor cannot call a method on its own prototype. Only the instance it creates can call a method on its prototype.

var p2 = new Person();
console.log(p2);
p2.sayHello();
console.log(p1.sayHello == p2.sayHello);  // true
console.log(p1.sayHello === p2.sayHello); // Because true is a method defined on the prototype of the constructor, each instance does not have the method itself. The method invoked by the instance comes from the prototype, and each instance invokes the same method.

 

For example, the Date object in javaScript has only three static methods: Date.now(), Date.parse(), and Date.UTC().

But there are many methods in the prototype of the Date object that can be called by the Date instance. The Date instance method can be divided into three categories: to class method, get class method and set class method.

For example, there are many static methods in the Array object, and there are also some methods and static methods with the same name in the prototype.

console.log(Array);

Some methods can be used on constructors and instances, but constructors and instances call the same, only between instances call the same. Take Array's concat method and Array's concat method as examples:

var arr = new Array;
var arr2 = new Array;
console.log(Array.concat == arr.concat);  // flase
console.log(arr.concat == arr2.concat);   // true
console.log(arr.concat === arr2.concat);  // true

 

Keywords: Javascript

Added by azaidi on Thu, 29 Aug 2019 16:00:40 +0300