How to quickly master JavaScript Object-oriented programming

Source: Internet
Author: User

  Guide: often see some JavaScript code is too messy to understand, everywhere are attributes and methods, or a loop with a loop. However, if you use object-oriented , you can clean the code well and make it easier to understand and modify the code. If you don't want your code to be understood only by God, try to consider using object-oriented patterns.


text of the translation:


everywhere are attributes, methods, the code is extremely difficult to understand, God, my programmer, what are you doing? Take a closer look at this tutorial and let's write elegant object-oriented JavaScript code !


As a developer, the ability to write elegant code is critical to your career. With the development of such technologies as node.js, you can even use JavaScript on the server side. In the same way, you can use JavaScript to control MongoDB persistent data storage.


Text Tags


Text markers are just a way to create objects in JavaScript, but there's certainly more to it than that, but it's the preferred method when you're only going to create an object instance.


var bill = {};


The code above is not practical, it is just an empty object. Next, we dynamically add some properties and methods to this object.


Bill.name = "Bill E Goat";


Bill.sound = function () {

Console.log (' bahhh!


};


This property name is added, and the value "Bill E Goat" is assigned to it. We don't need to create an empty object first, but we can write all the code directly in a pair of parentheses.


var bill = {


Name: ' Bill E Goat, '


Sound:function (} {


Console.log (' Bahhh ! ');


}


};


Is it beautiful? Accessing its properties and methods is as simple and natural as breathing.


Bill.name;//"Bill E Goat"


Bill.sound ();//"Bahhh"


If the property name is not a valid identifier, we can also access it:


bill[' name ';//"Bill E Goat"

Note: I added parentheses after the method was invoked. Now, let's 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've passed in the argument, and we've accessed it in the definition of the method. Next, add a new method to the object to access the Name property.


Bill.sayname = function () {


Console.log ("Hello" + this.name);


};


Bill.sayname ()///"Hello Bill E Goat"


We can access the property within the method using This.propertyname (in this case, this.name).


Bill.sayname;//function


What's going on? The access Sayname method returns a method definition. Now let's go deeper.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.