【问题标题】:How to restrict a child object from calling the parent function in JavaScript?如何限制子对象在 JavaScript 中调用父函数?
【发布时间】:2018-06-22 12:45:19
【问题描述】:

如何限制子对象在继承时不能调用父函数?

var Parent = function() {};

Parent.prototype.myF = function() {
  console.log('derp');
};

function Child() {};

//make [[prototype]] of Child be a ref to Parent.prototype
Child.prototype = Object.create(Parent.prototype);
Child.prototype.getName = function() {
  console.log('dddd')
}

var c = new Child();
c.myF();

我希望子类不能在Java 中尽可能调用myF(); 以使其成为private。我可以在 JavaScript 中执行此操作吗?

【问题讨论】:

标签: javascript inheritance


【解决方案1】:

它们还不是 javascript 中的私有字段,但目前 tc39 正在处理这个问题,您必须等待下一个 Ecmascript 标准 private fields,或者您可以使用转译器将您的代码转译成大脑可以处理的一些奇怪的东西不破译。但这可能会解决您的问题,您必须检查构造函数属性,它是父级还是子级,

function Parent() {};

Parent.prototype.doThis = function() {
    if ( this.constructor.name != "Parent" )
        throw new Error("You are not allowed to call this method");
    console.log("derp");
};

function Child() {};

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

Child.prototype.getName = function() {
    console.log("ddd");
};

let p = new Parent();
let c = new Child();


console.log(p.doThis());
console.log(c.getName());
console.log(c.doThis());

【讨论】:

    【解决方案2】:

    JavaScript 中没有私有成员。 但是,按照惯例,您可以模拟私有成员并模仿预期的行为。

    使用前缀

    function Child() {
        // underscore prefix
        this._var = 42;
    }
    
    var child = new Child();
    
    console.log(child.var); // undefined
    console.log(child._var); // 42

    function Child() {
        // underscore prefix
        this._prop = 42
    }
    
    Child.prototype.getProp = function() {
        return this._prop;
    };
    
    var child = new Child();
    
    console.log(child.prop); // undefined
    console.log(child._prop); // 42
    console.log(child.getProp()) // 42

    使用闭包

    function Child() {
        // this variable cannot be accessed 
        // directly from outside
        var prop = 42;
        
        this.getProp = function() {
            return prop;
        }
    }
    
    var child = new Child();
    
    console.log(child.prop); // undefined
    console.log(child.getProp()); // 42

    未来 JavaScript 中的私有方法

    this tc39 proposal 被接受并实现后,您将能够在 JavaScript 中定义私有方法和字段。


    你的情况

    var Parent = function() {};
    
    Parent.prototype._myF = function() {
      console.log('derp');
    };
    
    var Child = function() {};
    
    Child.prototype = Object.create(Parent.prototype);
    Child.prototype.getName = function() {
      console.log('dddd')
    }
    
    // usage
    
    var p = new Parent();
    // notice the underscore prefix (_)
    p._myF(); // "derp"
    
    var c = new Child();
    c.getName(); // "dddd"
    c.myF(); // Uncaught TypeError: c.myF is not a function

    但是您将能够通过子进程访问父函数_myF(),如下所示:c._myF();。这就是为什么在 JavaScript 的当前状态下,如果你想要私有成员,你必须遵守约定,每个带下划线前缀的成员都应该被认为是私有的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多