как написать oop javascript (с наследованием) в JetBrains Webstorm/PHPStorm, чтобы получить автодополнение/intellisense

Может ли кто-нибудь написать пример кода ООП JS, я полагаю, с аннотациями, где IDE JetBrains могут распознавать наследование? например

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

Каков наилучший способ?


person ya_dimon    schedule 11.09.2015    source источник


Ответы (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, например

person lena    schedule 14.09.2015