The four function of JavaScript object-oriented programming is also "class"

Source: Internet
Author: User
Tags object object static class

function is used in JavaScript to create functions or methods, but to implement object-oriented programming, classes are one of the indispensable roles and protagonists. But there is no class concept in JavaScript, and the so-called class is also simulated, and the class members and private members are modeled by function closures (for closures you can see across boundaries: closures). Here we will look at the "class" in JavaScript in a more down-to-earth way, bypassing some of the blunt principles.

Since you are using function to simulate a class, write code to create a keyword for the class or a function. We create a block punctuation class.

function Point() {
       this.X = 0;
       this.Y = 0;
};

var zeroPoint = new Point();
alert("当前座标值( X:" + zeroPoint.X + " , Y:" + zeroPoint.Y + " )");

We all know that the access of Non-static class members needs to be done through objects, so a point type object is created first, and then the x and y axis coordinates are accessed through the object. Syntactically, JavaScript object creation process is similar to C #, but the implementation mechanism is not the same. Here's new creates an object, which points to this new object object when the subsequent point () function executes. The this.x and This.y in point are two public members of the point class, so objects of point can be accessed directly from them.

When it comes to members of class members or objects, it is unavoidable to mention accessibility issues. In JavaScript classes, there are also public and private members, but the details are different. JavaScript Private members are also members who are not allowed to manipulate objects outside of the class, but private members can also be accessed within the class's internal members. Within the class generally only private members and private members can access each other, you can think that the other members of the authority is not enough to operate these private things, but if you have privileges, it is not the same, the private public replicable it is not mistaken. A private member variable is like a normal variable declaration, with the VAR keyword, a private method can declare a variable with Var to receive a method object, or it can be constructed like a normal method.

function Lady() {
       var age = 30;
       var name = "菜花";

       var think = function() {
         alert("其实我今年" + age + "岁。");
       };

       function fancy(){
         alert("幻想变成20岁。");
       };

       this.Introduce = function() {
         alert("我叫" + name + " , 今年20岁。");
       };
};

var younglady = new Lady();
alert(younglady.age);//结果undefined
younglady.think(); //不支持
younglady.fancy(); //不支持

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.