2015-09-11 6 views
0

Может кто-нибудь написать пример кода ООП JS, я предполагаю с аннотациями, где среда JetBrains может распознавать наследование? , например.Как написать oop javascript (с наследованием) в JetBrains Webstorm/PHPStorm получить autocompletion/intellisense

class Animal: 
prop: weight 
method: run() 

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

поэтому я хочу, чтобы WebStorm/PhpStorm autocompletes и показывает информацию (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

следующий будет работать без каких-либо аннотаций:

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, например

Смежные вопросы