用原型替换__proto__



我倾向于用c风格编写Javascript"类"。

在C#中(例如),我们做这个

public class Parent {
    // stuff
}
public class Child : Parent {
    // Protected and public stuff from Parent will be accessible
}

在JS中,我通过使用proto找到了类似的方法,例如

var namespace = namespace || {};
namespace.Parent = function() {
    // Public variables
    self.pubVariable = "I am accessible";
    // Private variables
    var priVariable = "I'm not accessible outside of self";
    // ctor
    function self() {}
    return self;
}

namespace.Child = (function() {
    this.__proto__ = new namespace.Parent();
    // ctor
    function self() {}
    self.init = function() {
        // Prints the pubVariable
        console.log(pubVariable);
    };
    return self;
})($);
// Call it (statically)    
namespace.Child.init();

虽然这是有效的,它只是Webkit和Mozilla。我知道使用原型可以实现这一点,但不知道如何实现。欢迎提出任何建议。谢谢

对于父类/子类,我会做这样的

// your parent class
var Parent = function() {
  // parent constructor
  console.log("parent constructor!");
  
  // some public properties
  this.foo = "foo";
  this.bar = "bar";
  // a private data member
  var secret = "123456";
};
// a parent function
Parent.prototype.something = function() {
  console.log("something!");
}
// your child class
var Child = function() {
  
  // call parent constructor
  Parent.call(this);    
  
  // child constructor
  console.log("child constructor!");
  // override bar
  this.bar = "override!";
};
// the magic!
// child prototype build from parent prototype
Child.prototype = Object.create(Parent.prototype, {constructor: {value: Child}});

示例用法

var c = new Child();
c.something();
// => parent constructor!
// => child constructor!
// => something!
c.foo //=> "foo"
c.bar //=> "override!"

如果您使用";名称空间";概念是相同的。


编辑

根据您的评论,这里有和添加的演示

var Foo = function(){};
Foo.prototype.hello = function(){ return "hello!"; };
var foo = new Foo();
// call our hello method
// this calls foo.__proto__.hello
foo.hello(); //=> "hello!"
// override `hello` method for this instance
foo.hello = function(){ return "こんにちは"; };
// call our hello method again
// this calls foo.hello because it is defined directly on our instance
// (it has a higher precedence in the lookup chain)
foo.hello(); //=> "こんにちは"
// remove the override
delete foo.hello;
// call our hello method again
// this goes back to calling foo.__proto__.hello
foo.hello(); //=> "hello!"
// remove the method prototype
delete Foo.prototype.hello
// call our hello method one last time
// spoiler: it's gone!
foo.hello(); //=> TypeError: Object [object Object] has no method 'hello'

正如您所看到的,通过使用this.something = function(){};在实例上直接定义方法,您将失去此功能。我个人更喜欢在原型上定义方法,因为增加了灵活性。通过这种方式,原型真的像蓝图一样工作。你得到了所有预定义的行为;如果需要,您可以进行修改,并随时恢复到原始状态,所有这些都是基于每个实例的。


还有一件事

在上一个例子中,我们有一个原型方法和一个实例方法重写。有没有办法也调用原始方法?让我们看看!

var Foo = function(){};
Foo.prototype.hello = function(){ return "hello!"; };
var foo = new Foo();
foo.hello = function(){ return "こんにちは!"; }
// call override method
foo.hello(); //=> "こんにちは!"
// call original method
Foo.prototype.hello.call(foo); //=> "hello!"
// japanese just one more time...
foo.hello(); //=> "こんにちは!" 

这也行,但我从来没有真正的需要。我想好处是你不需要这样知道原始类:)

// call original method from the instance
foo.__proto__.hello.call(foo); //=> "hello!"

原型!

我想,你想要这个

// namespace
var namespace = namespace || {};
// Parent Class
namespace.Parent = function() {
    this.pubVariable = "I am accessible";
    var priVariable = "I'm not accessible outside of this";
}
// Child class
namespace.Child = function() {
    // namespace.Parent.call(this);
    this.init = function()
    {
        // returns Parent class' pubVariable
        // inherited by namespace.Child.prototype
        return this.pubVariable;
    }
};
// inherit Parent class
namespace.Child.prototype = new namespace.Parent();
var kid = new namespace.Child();
console.log(kid.init()); // I am accessible

如果使用namespace.Parent.call(this),那么Child类将拥有自己的pubVariable副本,但现在Child类使用的是Parent的pubVariable

此外,如果您想将父类的methods与子类共享,那么您应该在父类的原型中添加方法,比如这个

namespace.Parent = function() { //... }
namespace.Parent.prototype.aMethodInParent = function(){ //... };

因此,当您将在像这样的子类中继承它时

namespace.Child = function() { // ... };
namespace.Child.prototype = new namespace.Parent();

另一个子/子类

namespace.AnotherChild = function() { // ... };
namespace.AnotherChild.prototype = new namespace.Parent();

在这种情况下,两个子类/子类都将使用其父类中相同的aMethodInParent()方法。

演示

最新更新