如何在JetBrains Webstorm / PHPStorm中编写oop javascript(带继承)来获取自动完成/智能感知

时间:2015-09-11 22:32:26

标签: javascript oop inheritance webstorm jetbrains-ide

有人可以写一个OOP JS代码示例,我假设有注释,JetBrains IDE可以识别继承吗? e.g。

class Animal: 
 prop: weight
 method: run()

class Cat extends Animal:
 prop: name
 method: eat()

所以我希望Webstorm / PHPStorm能够自动完成并显示信息(ctrl + q)以便进行此类操作:

 Cat.prototype.eat= function(){
     this.weight; //should be recognized as inherited property
 }

 var cat = new Cat();
 cat.run(); //should be recognized as inherited method

最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

以下内容无需任何注释即可使用:

var Animal = function () {
    this.weight = 0;
    this.run = function () {
    }
    return this;
};
var Cat = function () {
    this.name = "Tom";
    this.eat = function () {
        return "Nyam"
    }
    return this;
}

Cat.prototype = new Animal();
var cat = new Cat();

cat.run()

这种方式也有效:

function Shape() {
    this.x = 0;
    this.y = 0;
}

// superclass method
Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info('Shape moved.');
};

// Rectangle - subclass
function Rectangle() {
    Shape.call(this); // call super constructor.
}

// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

var rect = new Rectangle();

另外,您可以在此处使用JSDoc - 例如,请参阅http://usejsdoc.org/tags-augments.html