It is often seen that some JavaScript code is messy and unintelligible. attributes and methods are everywhere, or a loop is nested with a loop. However, if object-oriented code is used, the code can be easily understood and modified. If you do not want your code to be understood only by God, consider using the object-oriented mode whenever possible.
Attributes and methods are everywhere, and the code is very difficult to understand. Oh, my programmer, what are you doing? Take a closer look at this guide. Let's write elegant object-oriented JavaScript code together!
As a developer, writing elegant code is crucial to your career. With the development of technologies such as node. JS, you can even use JavaScript on the server. Similarly, you can use JavaScript to control MongoDB's continuous data storage.
Text mark
Text Markup is only a method for creating objects in Javascript. Of course, this is not the only method,It is the preferred method for creating only one object instance..
- VaR bill = {};
The above code is not practical. It is just an empty object. Next, we dynamically add some attributes and methods to this object.
- Bill. Name = "Bill e goat ";
- Bill. Sound = function (){
- Console. Log ('hhh! ');
- };
The property name is added and the value "Bill e goat" is assigned to it ". Instead of creating an empty object, we can write all the Code directly in a pair of parentheses.
- VaR bill = {
- Name: "Bill e goat ",
- Sound: function (){
- Console. Log ('hhh! ');
- }
- };
Is it beautiful? Accessing its attributes and methods is as simple and natural as breathing.
- Bill. Name; // "Bill e goat"
- Bill. Sound (); // "bahhh"
If the attribute name is not a valid identifier, we can access it as follows:
- Bill ['name']; // "Bill e goat"
Note: I added parentheses after the method. Now, we will rewrite the current sound method and add a parameter.
- Bill. Sound = function (Noise ){
- Console. Log (Noise );
- };
- Bill. Sound ("Brrr! "); //" Brrr! "He's cold :)
Well, we have passed in the parameter and accessed it in the method definition. Next, add a new method to the object to access the name attribute.
- Bill. sayname = function (){
- Console. Log ("hello" + this. Name );
- };
- Bill. sayname (); // "Hello bill e goat"
We can use this. propertyname (in this example, this. Name) to access the property within the method.
- Bill. sayname; // Function
What's going on? A method definition is returned when you access the sayname method. Now let's go deeper.
- VaR sound = bill. sound;
- Sound ('moo! '); // "Moo! "
Now we have specified the sound method as a local function. You can call this function and pass parameters. What do you think will happen when copying bill? (Similar to cloned goat Dolly)
- VaR Sally = bill;
- Sally. Name; // "Bill e goat", but its name is Sally
- Sally. Name = "Sally ";
- Sally. Name; // "Sally", better
- Bill. Name; // "Sally", the problem is transferred to Bill.
In the above example, we create a variable Sally and make it the same as Bill, so Bill and Sally will reference the same object in the memory. At this time, they will change at the same time. Let's take a look at the following code:
- Bill. Name = "Bill e goat ";
- Bill. sayname (); // "Hello bill e goat ";
- VaR sayname = bill. sayname;
- Sayname; // function all goes well so far
- Sayname (); // "hello", why is the name of Bill no longer displayed here?
The name of Bill is a local instance variable, which is only available to Bill. When the sayname method is created as a global variable, when it encounters this. Name declaration, it looks for the value of name globally. The only problem is that name is not defined yet. Now let's define the name globally to see what will happen:
- VaR name = "bearded octo ";
- Sayname (); // "Hello bearded octo"
The name variable is defined globally and the value "bearded octo" is assigned ". When we call the sayhello method, it searches for the name variable globally and obtains the value "bearded octo ". Why don't we create a common "class" for these objects?
Constructor
The constructor is another method for compiling object-oriented JavaScript code,When you need to initialize the attributes and methods of an object, or create instances with different attributes and methods, it will be your best choice.. Similarly, we start by creating an empty object:
- Function game (){};
This object includes its own attributes. You can even pass in the attributes when creating the object and then modify it.
- VaR Zelda = new game ();
- VaR SMB = new game ();
- Zelda. Title = "Legend of Zelda ";
- SMB. Title = "Super Mario Brothers ";
- Zelda. Title; // "Legend of Zelda"
- SMB. Title; // "Super Mario Brothers"
Now this object has its own method! When creating a new object, we can even pass the attribute and then modify it.
- Function game (title ){
- This. Title = typeof title! = 'Undefined '? Title :"";
- };
- VaR Zelda = new game ("Legend of Zelda ");
- Zelda. Title; // "Legend of Zelda"
- Zelda. Title = "ocarina of Time ";
- Zelda. Title; // "ocarina of time"
- VaR blank = new game ();
- Blank. Title ;//""
You may not understand the content of the second line. In fact, you use a ternary operation to avoid the if else of a single line. It is equivalent to the standard if else statement below.
- If (typeof title! = 'Undefined '){
- This. Title = title;
- } Else {
- This. Title = "";
- }
- // Is the same
- This. Title = typeof title! = 'Undefined '? Title :"";
If the title variable is input when the object is created, the title instance variable is specified as the title value. If no value is input, the title will be initialized to the default value "". We can also create a method to access this property:
- Zelda. lovetitle = function (){
- Console. Log ("I love" + this. Title );
- };
- Zelda. lovetitle (); // "I love ocarina of time"
This is neat, but we can do better. We can add a method to the game class so that all game class instances can use this method.
- Game. Prototype. heartit = function (){
- Console. Log ("I Heart" + this. Title );
- };
- Zelda. heartit (); // "I heart ocarina of time"
- SMB. heartit (); // "I heart Super Mario Brothers"
Postscript: of course, this is only an entry to JavaScript object-oriented programming. Oo JS has a lot to talk about. If you have anything you want to know, you can leave a message at the bottom of the author's blog.
Original article: beardedocto.tumblr.com