码迷,mamicode.com
首页 > Web开发 > 详细

js通过继承实现私有函数

时间:2016-07-23 22:58:07      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

本文是原创文章,如需转载,请注明文章出处

 

主要思想就是在继承时,只开放共有的属性和方法,不开放另外某些函数,从而实现私有的作用。

function A(){
    this.x = 100;
    this.y = 200;
    this.f = function(){
        console.log(this.x + this.y);
    }
  //私有函数
this.g = function(){ console.log(this.x - this.y); } } function C(){ var a = new A(); var o = new Object(); o.x = a.x; o.y = a.y; o.f = a.f; return o; } function B(){ } B.prototype = C(); function run(){ var b = new B(); b.f(); }

C函数的作用就是剔除A中的私有函数(g),将其他的属性和方法放入o对象返回,之后将B的原型赋值给C返回的对象,这样当生成B对象时,只能访问到x,y,f了,从而实现私有函数g

js通过继承实现私有函数

标签:

原文地址:http://www.cnblogs.com/Pickcle/p/5699654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!