Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Can javascript define the instance method?

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "can javascript define instance method". In daily operation, I believe that many people have doubts about whether javascript can define instance method. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "can javascript define instance method?" Next, please follow the editor to study!

Javascript can define instance methods: 1, using JavaScript object prototype to reference prototype to implement instance methods; 2, defining methods directly on object instances; 3, defining instance methods through this pointers.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

1. Use the JavaScript object prototype to reference prototype to implement the instance method.

Var BaseClass = function () {}; BaseClass.prototype.method1 = function () {alert ('This is an instance method');} var instance1 = new BaseClass (); instance1.method1 (); / / This is an instance method

2. Define the method (object) directly on the instance

Var BaseClass = function () {}; var instance1 = new BaseClass (); instance1.method1 = function () {alert ('This is an instance method too');} instance1.method1 (); / / This is an instance method too

3. Define instance methods (variables) through this pointers

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

So after defining the same instance method on instance, prototype reference and "this" at the same time, which one will the instance call first?

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

* by running the result tracking test, we can see that the priority of the variables directly smashed on the instance is higher than that defined on "this".

* variables defined on "this" that are higher than those defined by prototype

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

At this point, the study on "can javascript define the instance method" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report