This article mainly introduces Javascript object-oriented methods, including object method calls, private methods, static methods, public methods, and privileged methods, if you need a friend, you can see that JavaScript object-oriented is a concept that has been quite popular in recent years. As a result, the younger brother is not very knowledgeable. Although he has done many web projects and read a lot of profound information and tutorials on the internet, I still have a deep understanding of their profound theories. After reading some books some time ago, I finally got my own understanding. Today I also came out to pack it. If I think it is very esoteric, please despise me directly, if you do not think it is correct, make a brick directly.
First, let's get to know the following things.
Write function fn () {} Or var fn = function () {} In JS code. You can understand them as objects and arrays.
Before understanding object-oriented, let's take a look at the following.
1. Call of object Methods
The function written at the outermost layer of js can also be understood as a method of window objects. The Defined variables can also be called an attribute of the window object. For example:
Var test = function () {alert ("123")} You can also define function test () {alert ("123 ")}
We can call this method as a window object.
Test () // pop-up warning box 123 (because the window object is a top-level object, we can omit it)
Window. test () // pop-up warning box 123
Window ['test'] () // pop-up warning box 123, which can be understood as a method value under the window array object.
The above example shows how to use and call object methods.
2. Private Method
Private methods can only be used within the scope of an object. This can be understood in the form of variable scope.
The functions in the above example can be considered as the private method of the window object. Continue with the example below.
Var pet = function () {function showpet () {alert ("123")} showpet (); // Private methods can be used within the scope of the function. Var temp = "private variables can only be accessed within the scope of the function or object"} showpet (); // error pet. showpet () // still cannot call var Penguin = new pet () // instantiate a pet object Penguin. showpet () // sorry, this still cannot be called.
What if I want to define a method that can be called outside the scope of the object? How can I use a private method? Let's take a look at the next point.
3. Static Method
Let's continue with the above example with the above question.
Var pet = function () {function showpet () {// Private method alert ("123")} showpet (); // Private methods can be used within the function scope .} Pet. show = function () {// Add a static method to the pet object. Alert ("456")} pet. name = "Penguin" // Add a static property to the pet object. Pet. show () // pop-up warning box 456 alert (pet. name) // pop-up warning box Penguin // continue thinking collision ============================ var Penguin = new pet () // instantiate a pet object Penguin. show () // sorry, this static method does not seem to be inherited by the instance. It is commendable to have such ideas. Come on!
The above example shows you what static method is. Of course, you may not understand it. In fact, I don't understand it either, because I am also a cainiao, however, if you know how to write a static method for an object, you can call the static method. Maybe one day, you will come back to teach me. Let's take a look at the methods that can be called by instantiated objects with the above questions.
4. Public Methods
The public method is usually implemented by modifying the prototype of the constructor. After an object prototype is modified, all the object instances inherit the modifications of the prototype, if you feel fuzzy, ignore it ).
Modify the object prototype. Continue with the above example.
Pet. prototype. setname = function (str) {// Add a public method by modifying the prototype to add and modify the Instance Object's namename = str ;}
Example:
Var pet = function () {function showname () {// Private method alert (this. name)} this. show = function () {// if you do not understand it here, please note that this method will be introduced below. Showname () ;}} pet. prototype. setname = function (str) {name = str;} var Penguin = new pet () Penguin. setname ("Penguin"); // The name Of The added instance is Penguin. show (); // pop up Penguin. setname ("wind"); // The name Of The added instance is wind Penguin. show (); // bring up the wind
Run the code to play.
Script var pet = function () {name: "123", function showname () {// Private method alert (this. name)} this. show = function () {showname () ;}} pet. prototype. setname = function (str) {name = str;} var Penguin = new pet () Penguin. setname ("Penguin"); Penguin. show (); Penguin. setname ("wind"); Penguin. show (); script
5. Privileged methods (external interfaces of objects or functions)
In fact, we have used this method in the above example. This method can be called by instantiating Object Inheritance. Methods defined by using the thsi keyword within the constructor. Privileged methods can be publicly accessed outside the constructor (only for instantiated objects), and private members and methods can be accessed. Therefore, the interface used as an object or constructor is the most suitable, through privileged functions, we can control the access of public methods to private methods, which has many applications in JS framework extension.
You can refer to the following section as P. Let's take a look at how to define a privileged method, reference a privileged method, and continue to call the above example.
Var pet = function () {function showname () {// Private method alert (this. name)} this. show = function () {// use the this keyword to define a privileged method. Showname (); // access the private method in the privileged method;} pet. prototype. setname = function (str) {name = str;} var Penguin = new pet (); // instantiate a pet object Penguin. setname ("Penguin"); // call the public method to modify Penguin. show (); // call a privileged method to access a private method. The name is displayed.
First, you can use this. fn = function () {} in the constructor to create a privileged method. Access private methods in privileged functions;
The instantiated object can use some private methods by accessing the privileged functions. The methods for accessing the privileged functions are the same as those for accessing the public functions.
The first part of this article will be understood for the moment. The next part will use an instance to explain how object-oriented is installed with B.
The above is all the content of this article. I hope it will be helpful to you. If you are interested, you can refer to Javascript object-oriented-encapsulation. Thank you for your support for PHP Chinese network!