One advantage of (Javascript) prototype is also a disadvantage __prototype

Source: Internet
Author: User
One advantage of (Javascript)prototype is also a disadvantage
Article Origin: Csdn publish time: 2006-04-28 Views: 42

How inJavascriptImplement OO programming. I'm afraid the best way is to make full useprototypeProperty. AboutprototypeThere are a lot of introductions, I will not repeat. The more basic principle is that when you useprototypeAfter writing a class, when you new object, the browser will automaticallyprototypeTo attach the contents of the object to you. In this way, by usingprototype, you have achieved an OO-likeJavascript。 InJavascript, object is a associative array. A function is a class. When you write the following function, you actually define a class, which is its constructor.               function MyObject (name, size) {this.name = name;        this.size = size; After that, you can easily pass the MyObject classprototypeproperty to easily expand it.        For example, you can add other attributes and methods to him. MyObject.prototype. tellsize = function () {return ' size of ' +this.name+ ' is ' +this.size; } MyObject.prototype. color = "Red"; MyObject.prototype. Tellcolor = function () {return ' color of ' +this.name+ ' is ' +this.color;        var myobj1 = new MyObject ("Tiddles", "7.5 meters"); Domdiv.innerhtml + + myobj1.tellcolor () + "<br/><br/>"; As you can imagine, when you call the Tellcolor () method, the result is this: color of tiddles is red very handy,prototypeProperties can be added dynamically. For example, you need to add a height attribute to the MyObject and expect it to provide a tellheight () method to get the value of the Height property. You can continue to add the following code after the above code: MyObject.prototype. Height = "2.26 meters"; MyObject.prototype. tellheight = function () {return ' height of ' +this.name+ ' is ' +this.height; After that, you can visit the Myobj1 tellheight () method, and you can get the following results: The height of tiddles is 2.26 metersprototypeThese dynamic features look a bit glamorous, but I feel a bit chill. Indeed, these features give you a lot of flexibility and can give you the ability to runtime changes in class attributes and methods. However, a little bit of digging, there will be some bad habits generated. First, if you can add properties and methods dynamically, it's easy to imagine that when I call, I want to invoke a property or method that doesn't exist. This is a very serious question, and if we don't have the property or method at all when we call it, it could cause our script to drop. But there are solutions. For example, in the above code, when there is no Tellheight () method, we can write the following code to avoid errors:        if (myobj1.tellheight)        {               domdiv.innerhtml + + myobj1.tellheight () + "<br/><br/>";       } note, be sure to in the IF statement, do not add the right behind the method (), otherwise, directly down. Interested readers can print and see what the difference is between accessing Myobj1.tellheight and Myobj1.tellheight () separately. Maybe, you think it's a small meaning. Just add a judgment, not a good one. Yes, but the following is a more troubling problem. The problem of properties and methods is simple, but the problem of changing the properties and methods is serious. We can detect it without changing it. For example, consider the following code:        function MyObject (name, size)        {              this.name = name;                this.size = size;       }               MyObject.prototype. color = "Red"; MyObject.prototype. Tellcolor = function () {return ' color of ' +this.name+ ' is ' +this.color;        var myobj1 = new MyObject ("Tiddles", "7.5 meters");               Domdiv.innerhtml + + myobj1.tellcolor () + "<br/><br/>"; MyObject.prototype. color = "green"; Domdiv.innerhtml + + myobj1.tellcolor () + "<br/><br/>"; The code will produce the following result: Color of tiddles is red
Color of Tiddles is green Note that you are modifying the color property of the class MyObject. But you're surprised to see you. The attribute value of the object myobj1 that was instantiated has also changed. Days. If your project code is more than one person, then maybe someone will modify your class in order to plan for themselves. So, everyone's object has changed. So you're stuck in a long process of debug ...               (Don't say I didn't tell you.) above are attributes, and methods: function MyObject (name, size) {this.name = name;        this.size = size; } MyObject.prototype. color = "Red"; MyObject.prototype. Tellcolor = function () {return ' color of ' +this.name+ ' is ' +this.color;        var myobj1 = new MyObject ("Tiddles", "7.5 meters");               Domdiv.innerhtml + + myobj1.tellcolor () + "<br/><br/>"; MyObject.prototype. color = "green"; MyObject.prototype. Tellcolor = function () {return ' Your color of ' +this.name+ ' is ' +this.color; } domdiv.innerhtml + = Myobj1.tellcolor () + "<br/><br/>"; The result of this code is that the color of Tiddles is red
Your color of Tiddles is green ha. The original method can also change, sweat. Here's the problem.JavascriptToo flexible a way to program how many people do not adapt. No one would make such a mistake if the whole team had a higher level. However, when a young man does not know, the unauthorized modification of the class will cause all the objects of the person to change, whether the attribute or method. InJavascriptThe code has become more and more of the Ajax era, which is a serious problem. This explains that writingJavascript, a good programming style is more important. Remember someone once said this, think of Java and C # These more stringent language, although reduced flexibility, but also reduce the possibility of making mistakes. In this way, even a novice, he wrote the code will not be too much difference with the master. But, likeJavascriptSuch scripting language, because too flexible, so, the master wrote the Angel, and the new handwriting, may be the devil.
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.