Can JavaScript define instance methods?

青灯夜游
Release: 2021-11-22 14:03:18
Original
1763 people have browsed it

Javascript can define instance methods. Methods: 1. Use JavaScript object prototype reference prototype to implement instance methods; 2. Define methods directly on object instances; 3. Define instance methods through this pointer.

Can JavaScript define instance methods?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

1. Use JavaScript object prototype reference prototype to implement instance methods

var BaseClass = function() {};  
BaseClass.prototype.method1 = function(){  
      alert(' This is a instance method ');  
}  
var instance1 = new BaseClass();  
instance1.method1(); //This is a instance method
Copy after login

2. Directly define methods (objects) on instances

var BaseClass = function() {};  
var instance1 = new BaseClass();  
instance1.method1 = function(){  
    alert(' This is a instance method too ');  
}   
instance1.method1();//This is a instance method too
Copy after login

3. Define instance methods (variables) through this pointer

var BaseClass = function() {  
 this.method1 = function(){  
   alert(' Defined by the "this" instance method');  
  }  
 };  
var instance1 = new BaseClass();  
instance1.method1();//Defined by the "this" instance method
Copy after login

Then at the same time, define the same instance on the instance, prototype reference and "this" After the method, which one will be called first by the instance?

var BaseClass = function() {  
this.method1 = function(){  
       alert(' Defined by the "this" in the instance method');  
 }  
};  
var instance1 = new BaseClass();  
instance1.method1 = function(){  
    alert(' Defined directly in the instance method');  
}  
BaseClass.prototype.method1 = function(){  
    alert(' Defined by the prototype instance method ');  
}  
instance1.method1();//Defined directly in the instance method
Copy after login

* By running the result tracking test, we can see that the priority of variables directly on the instance is higher than those defined on "this";

* And the variables defined on "this" are higher than the variables defined by prototype;

* That is, the variables defined directly on the instance will overwrite the variables defined on "this" and variables defined by prototype, those defined on "this'" will override variables defined by prototypetype.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of Can JavaScript define instance methods?. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!