JavaScript OOP Instance-probe

Source: Internet
Author: User

<script>/* all detectors have a method of detection and analysis of the current browser environment, whether it is a browser or nodejs*//*container container detector *//*link link Detector *//* The outer layer is wrapped with an anonymous function that executes immediately, preventing some function declarations or variables from leaking out to the outside */!function (global) {    function detectorbase ( configs) {//Do not let the external call by direct call, must use new, do not use new will be error         /* use new words, This is the last object to return, This instanceof detectorbase should return true, not the description is not called directly through new */         if (!this instanceof detectorbase) {/**/             throw new error (' do not invoke without new. ');         }        this.configs= configs;/* all detectors will have the Config attribute */        this.analyze ();/* All probes need to parse the data when they are initialized */    }    detectorbase.prototype.detect=function () {/* represents an abstract probing method, and the base class is not a specific probe so the implementation is meaningless to illustrate the need to implement such a method */&nbsP;       throw new error (' not implemented ');     }    detectorbase.prototype.analyze=function () {         console.log (' Analyzing ... ');         this.data= "# # # data### ";     }    /*** Concrete Example ***/    function  Linkdetector (links) {/* link probe, the same as must be constructed by new */        detectorbase.apply ( this,arguments);         if (!this instanceof linkdetector) {             throw new error (' Do not  invoke without new. ');         }        this.links= Links;    }    function containerdetector (Containers) {        detectorbase.apply (this,arguments);         if (!this instanceof containerdetector) {             throw new error (' do not invoke without new. ');         }         this.containers=containers;    }    //inherit first     /*linkdetector and Containerdetector are likely to mount some of their own methods      need to pay attention to, must first implement the prototype chain inheritance, and then expand.      because the inheritance of the time to rewrite Linkdetector prototype property */    inherit (Linkdetector, Detectorbase);     inherit (containerdetector,detectorbase);     //expand  later    linkdetector.prototype.detect=function () {         console.log (' LOADING&NBsp;data: ' +this.data);         console.log (' Link detection  Started. ');         console.log (' scaning links: ' +this.links);     }    containerdetector.prototype.detect=function () {         console.log (' Loading data: ' +this.data);         console.log (' container detection started. ');         console.log (' scaning containers: ' +this.containers);     }    //prevent from being altered     /* Do not want the monitoring program to be rewritten, non-removable, non-extensible, non-writable */    object.freeze (detectorbase);     Object.freeze (Detectorbase.prototype);     object.freeze (Linkdetector);     object.freeze (linkdetector.prototype);      Object.freeze (Containerdetector);     object.freeze (Containerdetector.prototype);     //export to global object    /* Exposing 3 classes to the outer surface through defineproperties, while protecting them from being enumerated, cannot be deleted and rewritten */    object.defineproperties (global,{         LinkDetector:{value:LinkDetector},         ContainerDetector:{value:ContainerDetector},         detectorbase:{value:detectorbase}    });    function  Inherit (subclass,superclass) {//        subclass.prototype= Object.create (Superclass.prototype);         subclass.prototype.constructor=subclass;    }} (This);var cd=new  Containerdetector (' #abc   #def   #ghi '); Var ld=new linkdetector (' HTTP://WWW.TAOBAO.COM&NBsp;http://www.tmall.com http://www.baidu.com '); Cd.detect (); Ld.detect ();</script> 

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Operation Result:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/315302/201701/315302-20170110145246072-336820673. PNG "style=" margin:0px;padding:0px;border:0px; "/>


JavaScript OOP Instance-probe

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.