私有子方法的原型 [英] Prototype for private sub-methods

查看:90
本文介绍了私有子方法的原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下代码:

var baseClass = function() {
// CODE
    var subClass = function() {
        // MORE CODE
    }
}

将方法添加到baseClass很好,我只是使用

Adding methods to baseClass is fine, I just use

baseClass.prototype.newMethod = function () {
    // NEW CODE
}

我的问题是我应该如何向子类添加方法?使其简单地成为公共方法的唯一方法是吗?

My question is how should I add methods to subClass? Is the only way to simply make it a public method?

好的,所以我重新排列了代码,使子类不在baseClass之外.我传入baseClass,以便subClass仍然可以访问baseClass实例的属性.

OK so I've rearranged the code so the subClass is outside the baseClass. I pass in baseClass so subClass can still access the properties of the instance of baseClass.

var baseClass = function() {
    var base = this;
    this.property_a = 1;
    this.property_b = 5;

    var sub = new subClass(base);
    // CODE
}

var subClass = function(parent) {
    var sub = this;
    this.property_c = 1;
    this.method_a = function() {
        return sub.property_c + parent.property_a;
    }
    // MORE CODE
}

这很好并且可以工作,但是现在我遇到了一个新问题,当我使用原型添加方法时:

this is fine and works, but now I have a new problem of when I add a method using prototype:

subClass.prototype.method_b = function(){
    return sub.property_c + parent.property_b;
}

我收到一条错误消息,说未定义父级.

I get an error saying parent isn't defined.

基本上,我有一个相当简单的Web应用程序,它具有两个方面,即查看侧和编辑侧.我构建了一个基础类,其中包含查看所需的所有内容,并且我想在另一个文件中添加进行编辑所需的方法,以便仅当用户在编辑页面上时才加载它们.

Basically I have a fairly simple web application that has two sides, a viewing side and an editing side. I build the base class which includes everything necessary for viewing, and I want to add the methods required for editing in a different file so they're only loaded when a user is on the editing page.

推荐答案

为什么要在基类中声明该子类?对我来说没有道理.

Why do you declare that subclass in the base class? Doesn't make sense to me.

您可以在范围内的任何地方添加到子类的原型.在您的代码中应该是

You can add to the subclass's prototype whereever it is in you scope. In your code it would be

var baseClass = function() {
// CODE
    var subClass = function() {
        // MORE CODE
    }
    subClass.prototype = {
        ...
    }
}

但是我建议将其从基类构造函数中删除.如果出于某种原因希望将其设为私有,请添加一个闭包:

But I'd suggest to put it out of the base class constructor. If you want it private for some reason, add a closure:

(function(){

    baseClass = function() { // public
        // CODE
    }
    baseClass.prototype = {...};

    var subClass = function() { // private
        // MORE CODE
    }
    subClass.prototype = Object.create(baseClass.prototype);
    subClass.prototype.newMethod = function () { ... }
})()


编辑以回答扩展的问题:


EDIT to answer the extended question:

哦,子类不继承自baseClass!我们曾期望过,否则可以将其包含在构造函数中.然后,可以将相同的原型添加到每个不同的subClass构造函数中:

Ah, subClass doesn't inherit from baseClass! We had expected that, otherwise it may be OK to have it inside the constructor. Then, the same prototype could have been added to each of the different subClass constructors:

var subproto = {
    method_b: = function(){
        // manipulate "this"
    },
    ...
};
function baseClass() {
    // some code
    function sub() {
        // is a constructor for subs which belong to this specif base intance
        ...
    }
    sub.prototype = subproto; // the sub constructors of each base instance
                              // have the same prototype
    var x = new sub(),
        y = new sub(); // example usage of the sub constructor
}
baseClass.prototype = {...}

否则,如果您想要一个通用的子构造函数(在函数baseClass之外),则可以给子实例所属的基本实例作为构造函数的参数,就像您所做的那样.当然,子方法(内部方法和外部方法)只能访问该基本实例的公共属性.

Else, if you want one common sub constructor (outside of function baseClass), you may give the base instance the sub belongs to as an argument to the constructor - as you did. Of course the sub (both internal and external methods) can only access public properties of that base instance.

您在重新排列的代码中犯的错误是您的原型(外部")方法试图从子构造函数访问私有的parent变量.如您所说,未定义说父母的错误".

The mistake you made in your rearranged code is that your prototype ("external") methods tried to access the private parent variable from the sub constructor. As you say, "error saying parent isn't defined".

var subClass = function(parent) {
    var sub = this;
    this.parent = parent; // make it public
    this.property_c = 1;
    this.method_a = function() {
        return sub.property_c + parent.property_a;
    }
    // MORE CODE
}
subClass.prototype.method_b = function(){
    // prototype functions can only access public properties
    // i.e. privileged methods, public attributes and other prototype properties
    return this.property_c + this.parent.property_b;
}

这篇关于私有子方法的原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆