JS modules, classes, inheritance, namespaces, private properties and other related concepts carding

Source: Internet
Author: User

JS is exactly a kind of object-based language, and pure object-oriented language (such as as) a little bit different, JS does not have the concept of class. Although there are inherited but prototype-based inheritance. As the previous segment became more and more valued, Jser used some of the features of JS to create something as playful as a purely object-oriented one. The following is a summary of some of the concepts in JS, a lot of things are from the blog park some Daniel, I write this article mainly want to take as and JS to do contrast, in order to facilitate as image to JS of the program members. This piece of article deals with concepts such as modules, classes, inheritance, private properties, and public properties.

1. Module
As we all know, as there is a class concept, each time a class is generated, the system will have a. As the end of the file, this file we call the class file, we will all about the implementation of the class of the property method in this file to write. So what about JS. After I read a lot of Daniel's blog, I learned that JS can also be divided into modules. There are two ways to do this.

MODULE1.JS1). (function (namespace) {    function log () {        console.log (' test ');    }    Namespace.run = function () {        log ();    };}) (Window.module1 = Window.module1 | | {});//module2.js2). var module2 = {    run:function () {         console.log (' Hi module ');}    }

From the two ways we can see that the individual module in JS is not necessarily a separate class, but rather a set of related logic written into a module, the purpose is to clear the program architecture and facilitate collaborative development.

2. Concept of Class
There are no classes in JS, but each function can use new to generate the object. That is to say, in JS each function can represent the concept of the class in the AS. So how do private methods in a class, public methods, private properties, and public properties be implemented. can see the code

function ClassA () {        var a = 1;//Here is declared with the var keyword in the function, representing the private property in ClassA, externally inaccessible        this.b = 2;//Here This is called, Represents a property that is exposed in ClassA.        C = 3;//The C declared here is not in ClassA, but in the global scope, equivalent to WINDOW.C = 3;        Function d () {//functions declared in this form are private functions and can only be called in ClassA, and window cannot be called.        Console.log (' function d ');    }    THIS.E = function () {//functions declared in this form, belonging to the public function        Console.log (' function e ');}    } var cla = new ClassA (); Console.log (CLA.A);//undefinedconsole.log (CLA.B);//2console.log (CLA.C);// Undefinedconsole.log (WINDOW.C);//3console.log (CLA.D);//undefinedconsole.log (CLA.E);//implementation of the OUTPUT function e Console.log ( WINDOW.D);//undefined



3. Inheritance
Aser all know that as inheriting a class is simple enough to use extends. But JS inside inheritance is the use of the prototype inheritance, what is the prototype inheritance it. JS inside the object has a prototype attribute, we use this property to implement inheritance. The code demonstrates the following:

function ClassB () {        this.a = ten;        This.run = function () {                Console.log (' function run is called ');}        } function ClassC () {}; Classc.prototype = new ClassB ();//So ClassC inherit ClassB properties and methods do not believe can experiment under var t = new ClassC (); Console.log (T.A);//See No, The output here is 10t.run ();//The output here is "function run is called" ClassC.prototype.proA = 34;console.log (T.proa);//34

4. Namespaces
How is the namespace in JS implemented?

var com = {};com.amswf = {};com.amswf.utils = {};com.amswf.utils.pageutil = function () {        this.getpageurl = function () {                return window.location.href;}        }

If the above concept is understood, we can write a class module like this

Pageutil.js (function (window) {        window.com = {};        com.amswf = {};        Com.amswf.utils = {};        Com.amswf.utils.PageUtil = function () {};        Com.amswf.utils.PageUtil.prototype.name = ' amswf ';        Com.amswf.utils.PageUtil.prototype.getPageURL = function () {                return window.location.href;        };        Other property methods can continue ... }) (window)

External use is as follows:
var util = new Com.amswf.utils.PageUtil ();
Console.log (Util.getpageurl ());//This will output the current page address
Interested friends, can go to my blog guest, http://amswf.com, there will be more things dedicated to everyone.

JS modules, classes, inheritance, namespaces, private properties and other related concepts carding

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.