JS implements private functions through inheritance

Source: Internet
Author: User

This article is an original article, if you want to reprint, please indicate the source of the article

The main idea is to only open common properties and methods when inheriting, and not to open other functions, so as to realize the function of private.

functionA () { This. x = 100;  This. y = 200;  This. F =function() {Console.log ( This. x + This. Y); }
Private Functions This. G =function() {Console.log ( This. x- This. Y); }}functionC () {varA =NewA (); varo =NewObject (); O.x=a.x; O.y=a.y; O.F=A.F; returno;}functionB () {}b.prototype=C ();functionrun () {varb =NewB (); B.f ();}

The function of the C function is to eliminate the private function in a (g), the other properties and methods into the O object return, and then the prototype B is assigned to the object returned by C, so that when the B object is generated, only access to x,y,f, thus implementing the Private function g

JS implements private functions through inheritance

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.