5 kinds of object-oriented writing __js in JS

Source: Internet
Author: User


Today in order to see how people on the site is similar to the Horizontal tab switch effect is how to achieve, in fact, this is based on the specific business, you can have different methods, and pay attention to what I said here is similar. See how some large Web site is how to achieve, just look at others write the JS code, look more and more do not understand, write the code, look like a little Java-oriented object-oriented programming, not previously seen, but not before not careful study, but to add some data to it, today, carefully studied, The result let me rise knowledge, the mother of JS actually have object-oriented writing (feel like the discovery of the new world, good excitement, friends mo jokes, have written friends may scold, this SB, incredibly even this do not know. I really don't know what the teacher said. I found this on the Internet to search some information, ready to learn.


First, learn from the object-oriented approach.

The following article is copied from a forum, the theme of the forum is: discuss these five kinds of writing, their advantages and disadvantages, which comparison norms, especially the last two, often seen.

Article from: http://www.iteye.com/topic/434462

Define circle class, owning member variable r, constant pi, and member function area ()

Java code//1th notation function Circle (r) {THIS.R = R;   } Circle.pi = 3.14159;   Circle.prototype.area = function () {return CIRCLE.PI * THIS.R * THIS.R;      var c = new Circle (1.0); Alert (C.area ());
Java code//2nd notation var Circle = function () {var obj = new Object (); Obj.            PI = 3.14159; Obj.area = function (r) {return this.      PI * R * r;   return obj;   var c = new Circle (); Alert (C.area (1.0));
Java code//3rd notation var Circle = new Object ();   Circle.pi = 3.14159; Circle.area = function (r) {return this.   PI * R * r; Alert (Circle.area (1.0));
Java code//4th notation var circle={"PI": 3.14159, ' area ': function (r) {return this.           PI * R * r;   }   }; Alert (Circle.area (1.0));
Java code//5th notation var Circle = new Function ("this. PI = 3.14159;this.area = function (r) {return r*r*this.      PI;} "); Alert ((New Circle ()). Area (1.0));
(It should be noted that the fifth method is a function rather than a function; In JavaScript methods, variables are case-sensitive)
Let's talk about these five kinds of writing, their advantages and disadvantages, which comparison norms, especially the last two, often seen.

Interested friends can take a look at this post, through the discussion, the more agreeable is the first one and the fourth way of writing, so later on in accordance with the two types of writing, other understanding is good, can understand the code that other people write can.


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.