简体   繁体   中英

Can Visual Studio's Javascript Intellisense be given a hint on the 'this' keyword?

I'm using the Phaser framework to create a game within Visual Studio (2015, Community).

While I can successfully get Intellisense for smaller projects, that aren't OO, when I try switching over to object orientated JavaScript, I lose the Intellisense.

boot.js

/// <reference path="../lib/phaser-2.4.4.js" />
var BasicGame = {
};

BasicGame.Boot = function (game) {
    // This gives me Intellisense as expected:
    game.input.maxPointers = 1;
};

BasicGame.Boot.prototype = {
    init: function () {
    },

    preload: function () {
    },

    create: function () {
        // This is valid, but I don't get Intellisense.
        // Can I help VS understand what type 'this' is?
        this.state.start('Preloader');
    }
};

Can I, in some way, assist Visual Studio (2015/Code/higher) so that it understands what type the 'this' keyword is, in the case of the create method?

Other relevant files, for those not familiar with the Phaser framework OO template:

index.html

<!DOCTYPE html>
<html>
<head>
    <title>HTML5 Shoot 'em Up in an Afternoon</title>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="favicon.ico" />
    <style type="text/css">
        body {
            background: #333;
            margin: 0;
        }
    </style>
    <script src="lib/phaser-2.4.4.min.js"></script>
    <script src="src/boot.js"></script>
    <script src="src/preloader.js"></script>
    <script src="src/mainMenu.js"></script>
    <script src="src/game.js"></script>
    <script src="src/app.js"></script>
</head>
<body>
    <div id="gameContainer"></div>
</body>
</html>

preloader.js, mainMenu.js and game.js are excluded for brevity. They contain setups much like boot.js, above.

app.js

window.onload = function () {
    var game = new Phaser.Game(800, 600, Phaser.AUTO, 'gameContainer');

    game.state.add('Boot', BasicGame.Boot);
    game.state.add('Preloader', BasicGame.Preloader);
    game.state.add('MainMenu', BasicGame.MainMenu);
    game.state.add('Game', BasicGame.Game);

    game.state.start('Boot');
};

For what it's worth, I decided a bit over a month ago to start looking at developing in TypeScript instead of vanilla JavaScript.

This resulted in the Intellisense I wanted, and programming in a way that's a bit more efficient for me.

Thanks to Claies for his comments on my question which made me more receptive when I saw that I could learn TypeScript and get what I wanted from the framework.

Keeping the question open in case there ends up being a way to do what was originally asked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM